Here is a working program that declares variables prior to a for-loop.
package main
import "fmt"
type brackets struct {
ch string
pos int
}
type stack []brackets
func main() {
p := brackets{ch: "a"}
st := make(stack,0)
for i := 0; i < 3; i++ {
p = brackets{ch: "a", pos: 1}
st = append(st, p)
fmt.Println(p)
fmt.Println(st)
}
}
I want to declare the same variables as part of the for-loop. How? Here is a faulty attempt.
package main
import "fmt"
type brackets struct {
ch string
pos int
}
type stack []brackets
func main() {
// p := brackets{ch: "a"}
// st := make(stack,0)
for st, p, i := make(stack,0), brackets{ch: "a"}, 0; i < 3; i++ {
p = brackets{ch: "a", pos: 1}
st = append(st, p)
fmt.Println(p)
fmt.Println(st)
}
}
I get the following error: syntax error: st, p, i := make(stack, 0), brackets used as value
I am puzzled, as it is very easy to declare standard types such as ints and strings. Why not my struct?