我正在尝试定义一个返回初始化映射的函数:
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)
}
我遇到的错误:
Line 6 col 21 error| expected '[', found '{'
Line 7 col 5 error| expected ']', found ':='
Line 11 col 3 error| expected declaration, found 'return'
谁能帮我弄清楚语法?还是我正在尝试做一些 Go 不会做的事情?
最佳答案
您需要声明整个类型,包括键和值类型。
func defaults() map[string]string {
…
}
关于function - 如何定义返回 map 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43095764/