Go 语言中读取[]byte数组,获取到的数组元素类型会是int,eg:
package main
import (
"fmt"
)
type IPAddr [4]byte
func main() {
test := IPAddr{123,'T',3,1}
temp := test[0]
fmt.Printf("%T %v\n", temp, temp)
temp = test[1]
fmt.Printf("%T %v", temp, temp)
}
输出:
uint8 123
uint8 84
将int类型转换为string,eg:
package main
import (
"fmt"
"strconv"
)
type IPAddr [4]byte
func main() {
test := IPAddr{123,12,3,1}
result := ""
for i:=0;i<len(test);i++{
result += strconv.Itoa(int(test[i]))
}
fmt.Printf("%T %v", result, result)
}
输出:string 1231231
将字符转化为string,eg:
package main
import (
"fmt"
)
type IPAddr [4]byte
func main() {
test := IPAddr{'T','e','s','t'}
result := ""
for i:=0;i<len(test);i++{
result += string(test[i])
}
fmt.Printf("%T %v", result, result)
}
输出:string Test