如何从指向字符串的指针获取字符串值?
我正在使用goopt包来处理标志解析,该包仅返回*string。我希望使用这些值来调用映射中的函数。
var strPointer = new(string)
*strPointer = "string"
functions := map[string]func() {
"string": func(){
fmt.Println("works")
},
}
//Do something to get the string value
functions[strPointerValue]()
返回值
./prog.go:17:14: cannot use strPointer (type *string)
as type string in map index
nil
,将会发生运行时错误。 - Matteo