dsfovbm931034814
2017-03-25 09:12
浏览 2.9k
已采纳

如何在Golang中将字符串转换为数组?

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"

to

["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.

图片转代码服务由CSDN问答提供 功能建议

如何在Go中有效地将字符串转换为带有一个元素(即该字符串)的字符串数组。

例如:

  var s string 
s =“这是一个字符串” 
    
 
 

  [“这是一个字符串”] 
   
 
 

很明显,一个 一种方法是创建一个字符串数组并将第一个元素初始化为该字符串,但我正在寻找一种有效的方法。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题