dongme8388 2017-01-17 16:19
浏览 643
已采纳

在Go中初始化空字符串的惯用方式

In Go you can initialise an empty string (in a function) in the following ways:

var a string
var b = ""
c := ""

As far as I know, each statement is semantically identical. Is one of them more idiomatic than the others?

  • 写回答

5条回答 默认 最新

  • drxt70655 2017-01-17 16:51
    关注

    You should choose whichever makes the code clearer. If the zero value will actually be used (e.g. when you start with an empty string and concatenate others to it), it's probably clearest to use the := form. If you are just declaring the variable, and will be assigning it a different value later, use var.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?