Go golang, syntax error: unexpected ++, expecting : - syntax

func test(args ...string) {
var msg map[string] interface{}
i := 0
msg["product"] = args[i++]
msg["key"] = args[i++]
msg["signature"] = args[i++]
msg["string_to_sign"] = args[i++]
}
go build utils.go
after compile, I get the error message
./utils.go:28: syntax error: unexpected ++, expecting :
./utils.go:28: missing statement after label
./utils.go:29: syntax error: unexpected ++, expecting :
./utils.go:30: syntax error: unexpected ++, expecting :
./utils.go:31: syntax error: unexpected ++, expecting :
./utils.go:36: syntax error: unexpected ++, expecting :
./utils.go:37: syntax error: unexpected ++, expecting :
why can't I put i++ in index of slice? is there any limitation in index of slice?

Go Frequently Asked Questions (FAQ)
Why are ++ and -- statements and not expressions? And why postfix,
not prefix?
Without pointer arithmetic, the convenience value of pre- and postfix
increment operators drops. By removing them from the expression
hierarchy altogether, expression syntax is simplified and the messy
issues around order of evaluation of ++ and -- (consider f(i++) and
p[i] = q[++i]) are eliminated as well. The simplification is
significant. As for postfix vs. prefix, either would work fine but the
postfix version is more traditional; insistence on prefix arose with
the STL, a library for a language whose name contains, ironically, a
postfix increment.
The Go Programming Language Specification
IncDec statements
The "++" and "--" statements increment or decrement their operands by
the untyped constant 1. As with an assignment, the operand must be
addressable or a map index expression.
IncDecStmt = Expression ( "++" | "--" ) .
The following assignment statements are semantically equivalent:
IncDec statement Assignment
x++ x += 1
x-- x -= 1
Write,
func test(args ...string) {
var msg map[string]interface{}
i := 0
msg["product"] = args[i]
i++
msg["key"] = args[i]
i++
msg["signature"] = args[i]
i++
msg["string_to_sign"] = args[i]
}
Which, in your particular case, simplifies to,
func test(args ...string) {
var msg map[string]interface{}
msg["product"] = args[0]
msg["key"] = args[1]
msg["signature"] = args[2]
msg["string_to_sign"] = args[3]
}

According to Language Specification, http://golang.org/ref/spec#IncDec_statements, i++ is a IncDec statements, which is a statement, but not a expression.As for args[index], index must be a expression. You want more details , just read it Go Language Specification, it's just what the language demand.

As other people have said i++ is a statement in go, not an expression as it is in C. Go has a different way of expressing the same intent using multiple assignment:
func test(args ...string) {
msg := make(map[string]string)
i := 0
msg["product"], i = args[i], i+1
msg["key"], i = args[i], i+1
msg["signature"], i = args[i], i+1
msg["string_to_sign"], i = args[i], i+1
fmt.Printf("%v\n", msg)
}
Your definition of map would have failed at runtime too.

++ operator is disappointing. This is my hack:
func test(args ...string) {
i := 0
inc := func(i *int) int { *i++; return i }
var msg map[string] interface{}
msg["product"] = args[inc(&i)]
msg["key"] = args[inc(&i)]
msg["signature"] = args[inc(&i)]
msg["string_to_sign"] = args[inc(&i)]
}

Related

Why is my initialization statement in `if` syntactically incorrect? [duplicate]

Any idea why this struct expression in for loop initializer makes syntax error in compile-time? Pointer to struct works fine in this case but ofc I need local variable like bellow. Thanks for advices!
type Request struct {
id int
line []byte
err error
}
go func() {
for r := Request{}; r.err == nil; r.id++ {
r.line, r.err = input.ReadSlice(0x0a)
channel <- r
}
}()
Simplifying you code:
for r := Request{}; r.err == nil; r.id++ {
r.line, r.err = input.ReadSlice(0x0a)
channel <- r
}
Gives compile time error:
expected boolean or range expression, found simple statement (missing parentheses around composite literal?) (and 1 more errors)
This construct is ambiguous to parse. The opening brace '{' is not obvious whether it is part of a composite literal or the opening brace of the for statement itself (the for block).
You can make it obvious by using parentheses around the composite literal (as the error suggests):
for r := (Request{}); r.err == nil; r.id++ {
r.line, r.err = input.ReadSlice(0x0a)
channel <- r
}

How to declare variable types for loop variables in Go?

See this code.
package main
import (
"fmt"
)
func main() {
var arr [4]string = [4]string{"foo", "bar", "baz", "qux"}
for a, b := range arr {
fmt.Println(a, b)
}
// How can I fix this code?
/*
for x int, y string = range arr {
fmt.Println(a, b)
}
*/
}
The first for loop uses the := operator to automatically deduce the types of a and b. But what if I want to explicitly specify the types of the loop variables? My attempt to do this is in the second block of commented code which of course failed with the following error.
# command-line-arguments
./foo.go:15: syntax error: unexpected name, expecting {
./foo.go:18: syntax error: unexpected }
Can you help me fix the second block of code such that I can specify the types of x and y explicitly?
Unfortunately the language specification doesn't allow you to declare the variable type in the for loop. The closest you could get is this:
var a int
var b string
for a, b = range arr {
fmt.Println(a, b)
}
But normally if you give your variable meaningful names, their type would be clear as well:
for index, element := range arr {
fmt.Println(index, element)
}
You need to declare first the vars.
var x int
var y string ...// now it should be ok.
for x,y = range arr {
fmt.Println(x, y) // it should be x and y instead of a,b
}
Check the fiddle
First of all your code is not a valid Go code. The for range loop returns the index and the value of an array, slice, string, or map, so there is no reason the explicitly specify the type of the value and the index.
You are specifying the type of the values at the variable initialization, and the language will deduce the type on the range iteration.
One special case is when you are using interface{} as variable type. In this case, you if you need to know the type of the value you can use the reflect package to deduce the type of the value.
switch reflect.TypeOf(t).Kind() {
case reflect.Slice:
s := reflect.ValueOf(t)
for i := 0; i < s.Len(); i++ {
fmt.Println(s.Index(i))
}
}
It's not possible as you are trying to declare two different types of data in same line, if you want explicitly declare variables, then you need to declare them before itself like above answers, but if you want them to be of other type then you need to covert them as for your needs,
package main
import (
"fmt"
)
func main() {
var arr = [4]string{"foo", "bar", "baz", "qux"}
var x int64
var b []byte
for x = 0; x < int64(len(arr)); x++ {
b = []byte(arr[x])
fmt.Println(x, b)
}
}

Shorthand return

The following code generates a syntax error (unexpected ++ at end of statement) in Go 1.6 or 1.7:
package main
import "fmt"
var x int
func increment() int {
return x++ // not allowed
}
func main() {
fmt.Println( increment() )
}
Shouldn't this be permitted?
It's an error, because the ++ and -- in Go are statements, not expressions: Spec: IncDec Statements (and statements have no results that would be returned).
For reasoning, see Go FAQ: Why are ++ and -- statements and not expressions? And why postfix, not prefix?
Without pointer arithmetic, the convenience value of pre- and postfix increment operators drops. By removing them from the expression hierarchy altogether, expression syntax is simplified and the messy issues around order of evaluation of ++ and -- (consider f(i++) and p[i] = q[++i]) are eliminated as well. The simplification is significant. As for postfix vs. prefix, either would work fine but the postfix version is more traditional; insistence on prefix arose with the STL, a library for a language whose name contains, ironically, a postfix increment.
So the code you wrote can only be written as:
func increment() int {
x++
return x
}
And you have to call it without passing anything:
fmt.Println(increment())
Note that we would be tempted to still try to write it in one line using an assignment, e.g.:
func increment() int {
return x += 1 // Compile-time error!
}
But this also doesn't work in Go, because the assignment is also a statement, and thus you get a compile-time error:
syntax error: unexpected += at end of statement
The accepted solution is right that the OP's code does not work because in go increment/decrement(x++/x--) statements are expressions that don't return a value.
However the solution presented has a slightly different effect than the original request.
x++ would return the value of x then increment in C like syntax.
however the opposite would happen if you do it this way:
x++
return x
You can negate that issue by reducing your initial value by one or by using a defer statement as written here:
func incr() int {
defer func() { counter++ }()
return counter
}
https://play.golang.org/p/rOuAv7KFJQw

Why can't I key in a string in Golang map?

I'm writing a function in go to remove duplicate characters in a string. Here is my approach. When I run the following test, why do I get this error? I'm new to Go and used to more dynamic languages like Ruby/Python.
panic: assignment to entry in nil map [recovered]
panic: assignment to entry in nil map
source.go
func removeDuplicate(s string) string {
var m map[string]int
var c_string []string = strings.Split(s, "")
for i :=0; i < len(c_string); i++ {
m[c_string[i]] = 0
}
for i :=0; i < len(c_string); i++ {
m[c_string[i]] = m[c_string[i]] + 1
}
var (
result string = ""
)
for i :=0; i < len(c_string); i++ {
if m[c_string[i]] < 1 {
result = result + c_string[i]
}
}
return result
}
source_test.go
func TestRemoveDuplicateChars(t *testing.T) {
got := removeDuplicateChars("abbcde")
if got != "abcde" {
t.Fatalf("removeDuplicateChars fails")
}
}
Because you haven't actually initilize/allocated m, you've only declared it. Make this; var m map[string]int into m := map[string]int{}.
Which does initilization and assignment both in the same statement. You could also add another line m = make(map[string]int) which would prevent the error though I personally prefer the compacted syntax.
fyi your code is barfing on this line; m[c_string[i]] = 0, the error message should make sense when combining that with the information above.

Why does adding parentheses in if condition results in compile error?

The following Go code runs OK:
package main
import "fmt"
func main() {
if j := 9; j > 0 {
fmt.Println(j)
}
}
But after adding parentheses in condition:
package main
import "fmt"
func main() {
if (j := 9; j > 0) {
fmt.Println(j)
}
}
There is compile error:
.\Hello.go:7: syntax error: unexpected :=, expecting )
.\Hello.go:11: syntax error: unexpected }
Why does the compiler complain about it?
The answer is not simply "because Go doesn't need parentheses"; see that the following example is a valid Go syntax:
j := 9
if (j > 0) {
fmt.Println(j)
}
Go Spec: If statements:
IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt | Block ) ] .
The difference between my example and yours is that my example only contains the Expression block. Expressions can be parenthesized as you want to (it will not be well formatted, but that is another question).
In your example you specified both the Simple statement and the Expression block. If you put the whole into parentheses, the compiler will try to interpret the whole as the Expression Block to which this does not qualify:
Expression = UnaryExpr | Expression binary_op UnaryExpr .
j > 0 is a valid expression, j := 9; j > 0 is not a valid expression.
Even j := 9 in itself is not an expression, it is a Short variable declaration. Moreover the simple assignment (e.g. j = 9) is not an expression in Go but a statement (Spec: Assignments). Note that assignment is usually an expression in other languages like C, Java etc.). This is the reason why for example the following code is also invalid:
x := 3
y := (x = 4)
Because that is how the Go syntax defines an if statement.
IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt |
Block ) ] .
And from Effective Go:
Parentheses
Go needs fewer parentheses than C and Java: control
structures (if, for, switch) do not have parentheses in their syntax.
and:
Control structures
The control structures of Go are related to those of C but differ in
important ways. There is no do or while loop, only a slightly
generalized for; switch is more flexible; if and switch accept an
optional initialization statement like that of for; break and continue
statements take an optional label to identify what to break or
continue; and there are new control structures including a type switch
and a multiway communications multiplexer, select. The syntax is also
slightly different: there are no parentheses and the bodies must
always be brace-delimited.
(Emphasis added)

Resources