小编典典

如何将“字符串指针”转换为 Golang 中的字符串?

go

是否可以从指向字符串的指针获取字符串值?

我正在使用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

阅读 211

收藏
2021-12-27

共1个答案

小编典典

取消引用指针:

strPointerValue := *strPointer
2021-12-27