我是 golang 的新手,想找到一種定義單個 byte變量的方法。這是Effective Go參考中的一個演示程序。package mainimport ( "fmt")func unhex(c byte) byte{ switch { case '0' <= c && c <= '9': return c - '0' case 'a' <= c && c <= 'f': return c - 'a' + 10 case 'A' <= c && c <= 'F': return c - 'A' + 10 } return 0}func main(){ // It works fine here, as I wrap things with array. c := []byte{'A'} fmt.Println(unhex(c[0])) //c := byte{'A'} **Error** invalid type for composite literal: byte //fmt.Println(unhex(c))}如您所見,我可以用數組包裝一個字節,一切正常,但是如何在不使用數組的情況下定義單個字節?謝謝。
- 2 回答
- 0 關注
- 258 瀏覽
添加回答
舉報
0/150
提交
取消