dsfovbm931034814 2017-03-25 09:12
浏览 4814


How to convert a string to an array of strings with one element (i.e. that string) in Go effectively.

For example:

var s string
s = "This is a string"


["This is a string"]

Obviously, one way would be to make an array of strings and initialize the first element as that string but I am looking for an effective approach.

  • 写回答

1条回答 默认 最新

  • dtz55359 2017-03-25 09:21

    To initialize a string slice in Go, you use s := []string{"This is a string"}.
    To initialize a string array in Go, you use s := [1]string{"This is a string"}.

    The only difference (in declaring each) lies in specifying the array length or not.

    To understand which structure you want to use, you should read more about the difference between slices and arrays on the Go Blog.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 电脑开机过商标后就直接这样,求解各位
  • ¥15 mysql , 用自己创建的本地主机和用户名 登录不上
  • ¥15 关于#web项目#的问题,请各位专家解答!
  • ¥15 vmtools环境不正常
  • ¥15 请问如何在Ubuntu系统中安装使用microsoft R open?
  • ¥15 jupyter notebook
  • ¥30 informer时间序列预测
  • ¥20 SSR引物多态性分析
  • ¥15 大漠插件在Win11易语言注册调用和免注册灵异事件,VS上注册调用完全没问题
  • ¥15 Addressable缓存机制做热更新的问题