Getting syntax error: unexpected comma, pending {

I tried, broke and continued in the golang, and I did it ...

func main() {
    for k, i := 0, 0; i < 10; i++, k++ {
        for j := 0; j < 10; j++ {
            if k == 10 {
                fmt.Println("Value of k is:", k)
                break
            }
        }
    }
}

I get this syntax error on line 1 for:

Syntax error: unexpected comma, pending {

I don't know what the correct syntax should look like.

+4
source share
1 answer

You need to initialize both k, and i:for k, i := 0, 0;

In addition, you can not do: i++, k++. Instead you need to doi, k = i+1, k+1

See this link in the Effective Path section :

, Go ++ - . , ( ++ -).

// a

for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 { a[i], a[j] = a[j], a[i] }

func main() {
    for k, i := 0, 0; i < 10;  i, k = i+1, k+1 {
        for j := 0; j < 10; j++ {
            if k == 10 {
                fmt.Println("Value of k is:", k)
                break
            }
        }
    }
}

, k 10, , . i k, i < 10 (, , k < 10).

+9

All Articles