I am attempting to define a function which returns an initialized map:
package main
import "fmt"
import "os"
func defaults() map {
m := make(map[string]string)
m["start"] = "1"
m["from"] = "encrypted"
m["to"] = "loaded"
return m
}
func main() {
args := os.Args[1:]
fmt.Println(args)
runvals := defaults()
fmt.Println(runvals)
}
Errors I'm getting:
Line 6 col 21 error| expected '[', found '{'
Line 7 col 5 error| expected ']', found ':='
Line 11 col 3 error| expected declaration, found 'return'
Can someone help me get the syntax right? Or am I trying to do something that Go doesn't do?
func defaults() map[string] string {
m := make(map[string]string)
m["start"] = "1"
...
return m
}
The problem with your defaults function is the return type map has no types.
You need to declare the whole type including key and value types.
func defaults() map[string]string {
…
}