duanhongyi2964 2014-11-12 16:20 采纳率: 100%
浏览 241
已采纳

如何在Go中使用基础字符串值初始化类型?

This works:

type T string
var t T = "hello"

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

But this fails with 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

How can I make this second situation work?

  • 写回答

1条回答 默认 最新

  • drvxnivoqf17568697 2014-11-12 16:22
    关注

    Convert the string to the correct type [conversions]

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

    s := "world"
    t := T(s)
    fmt.Println(t)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到