回答

收藏

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

技术问答 技术问答 400 人阅读 | 0 人回复 | 2023-09-11

字符串值可以从指向字符串的指针中获得吗?
! e7 B1 t3 D# R% Z0 e5 ]- h% e我正在使用goopt 包处理标志分析,包只返回 *string。我想用这些值来调用地图上的函数。; k/ _+ ^6 n( f4 k  t
例子
: c9 X  u# P% L" C5 x. Y
    var strPointer = new(string)*strPointer = "string"functions := map[string]func()      "string": func()()(()()()()(                      )fmt.Println("works")   Do something to get the string valuefunctions[strPointerValue]()
    ; ^7 Q6 J0 E* W) K
回报
/ b: m- G% @: E
    ./prog.go:17:14: cannot use strPointer (type *string) as type string in map index
    . I' B; i' N! V4 \5 n
               
. G. L# d# |( v: D3 Q    解决方案:                                                               
$ A  h4 T! }" {7 s% ?                                                                取消引用指针:
& x: b1 j# ~4 _7 {' B. H$ O3 V/ J
    strPointerValue := *strPointer
    , R% W! }) o; ?% x* z
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则