dongmeixian9665 2018-08-12 10:29
浏览 58
已采纳

Go无法从字符串创建全局字节片

I am trying to make a global byte array from a string with:

var operators = []byte {"+-*/%"}

however, I am getting the error

cannot use string("+-*/") (type untyped string) as type byte in array or slice literal

What am I doing wrong here?

  • 写回答

2条回答 默认 最新

  • douwen1213 2018-08-12 10:51
    关注

    Use a type conversion to convert a string to a slice of bytes. Note the use of () instead of {}.

     var operators = []byte("+-*/%")
    

    The code in the question is a composite literal.

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

报告相同问题?