如何将“字符串指针”转换为 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. Yvar 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/ JstrPointerValue := *strPointer
, R% W! }) o; ?% x* z |
|
|
|
|
|