回答

收藏

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

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

字符串值可以从指向字符串的指针中获得吗?
6 H; {2 {4 U9 f# S1 U我正在使用goopt 包处理标志分析,包只返回 *string。我想用这些值来调用地图上的函数。
4 ^/ ?4 W" z! S8 \$ c例子, t# U4 j) c8 g4 Y0 q2 c
    var strPointer = new(string)*strPointer = "string"functions := map[string]func()      "string": func()()(()()()()(                      )fmt.Println("works")   Do something to get the string valuefunctions[strPointerValue]()$ o* F- R4 M4 _
回报
( X1 P# ?' i' Z4 g- w/ e
    ./prog.go:17:14: cannot use strPointer (type *string) as type string in map index
    : |, a! C5 j; d6 {) C& e- w" s8 n
               9 Z- }' ]4 g9 B% }7 @  [
    解决方案:                                                                9 Y- x2 O% P: j, B; f2 n5 P
                                                                取消引用指针:
$ m  Z& O% J/ n+ s! U/ p
    strPointerValue := *strPointer
    1 |  b$ a6 [: W  W/ U+ O
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则