回答

收藏

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

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

字符串值可以从指向字符串的指针中获得吗?% j( `* K4 ~5 g8 L$ f
我正在使用goopt 包处理标志分析,包只返回 *string。我想用这些值来调用地图上的函数。
6 q/ [5 ~5 U/ h3 g. D例子6 N, W3 L( u" V  d8 {; \
    var strPointer = new(string)*strPointer = "string"functions := map[string]func()      "string": func()()(()()()()(                      )fmt.Println("works")   Do something to get the string valuefunctions[strPointerValue]()
    7 t+ g; |: n) o3 E( D  Z
回报6 W6 x) ?3 ?- `/ M" m" |
    ./prog.go:17:14: cannot use strPointer (type *string) as type string in map index: D# b/ c% m# Q3 i9 u1 r
               ' f+ t# j3 q3 T3 o# e8 S
    解决方案:                                                               
# T# I: [+ m& A                                                                取消引用指针:/ L! u" I) @+ [7 O. j. b$ Z
    strPointerValue := *strPointer
    9 h2 T' y  ?3 s# Y
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则