gpt4 book ai didi

go - 如何在 go 中初始化具有底层字符串值的类型?

转载 作者:IT王子 更新时间:2023-10-29 02:30:14 26 4
gpt4 key购买 nike

这个有效:

type T string
var t T = "hello"

http://play.golang.org/p/275jQ4ixvp

但这失败了 cannot use s (type string) as type T in assignment

type T string
s := "world"
var t T = s

http://play.golang.org/p/vm3mC5ltcE

我怎样才能使第二种情况起作用?

最佳答案

将字符串转换为正确的类型[ conversions ]

http://play.golang.org/p/dkavI_QgPb

s := "world"
t := T(s)
fmt.Println(t)

关于go - 如何在 go 中初始化具有底层字符串值的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26891629/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com