1、数据类型转换
(1)Golang 定长字节数组[N]byte 转 字符串string
arrayByte := [5]byte{0, 1, 2, 3, 4}
sliceByte := []byte{0, 1, 2, 3, 4}
//数组转字符串
arrayStr := string(arrayByte[:])
fmt.Println("定长数组(数组)转字符串:", arrayStr)
arrayStr = fmt.Sprintf("%x", arrayStr)
fmt.Println("定长数组(数组)转字符串(十六进制):", arrayStr)
//切片转字符串
sliceStr := string(sliceByte)
fmt.Println("变长数组(切片)转字符串:", sliceStr)
sliceStr = fmt.Sprintf("%x", sliceStr)
fmt.Println("变长数组(切片)转字符串(十六进制):", sliceStr)
fmt.Println("定长数组(数组)元数据类型:", reflect.TypeOf(arrayByte))
fmt.Println("变长数组(切片)元数据类型:", reflect.TypeOf(sliceByte))
输出结果:
定长数组(数组)转字符串:
定长数组(数组)转字符串(十六进制): 0001020304
变长数组(切片)转字符串:
变长数组(切片)转字符串(十六进制): 0001020304
定长数组(数组)元数据类型: [5]uint8
变长数组(切片)元数据类型: []uint8
(2)int int64 string相互转换
str := "16"
//string > int
integer, err := strconv.Atoi(str)
if err != nil {
fmt.Println("stringl > int error:", err)
}
//string > int64
integer64, err := strconv.ParseInt(str, 10, 64)
if err != nil {
fmt.Println("stringl > int64 error:", err)
}
//int > string
str = strconv.Itoa(integer)
//int64 > string
str = strconv.FormatInt(integer64, 10)
//int > int64
intTmp := int64(integer)
(3)int64、[]byte互转
var val int64 = 2000
//int64 -> []byte
byteVal := big.NewInt(val).Bytes()
fmt.Println(byteVal)
//[]byte -> int64
val = big.NewInt(0).SetBytes(byteVal).Int64()
fmt.Println(val)
输出结果:
[7 208]
2000
2、字符串替换(strings.Replace)
str := "You are Tracy, and you are so cute!"
//最后的参数表示替换次数
newStr := strings.Replace(str," are", "`re",1)
fmt.Println("原字符串:",str)
fmt.Println("新字符串:",newStr)
输出结果:
原字符串: You are Tracy, and you are so cute!
新字符串: You`re Tracy, and you are so cute!
3、整数类型最大长度
var uint64Max uint64 = math.MaxUint64
fmt.Printf("uint8 : 0 - %d\n", math.MaxUint8)
fmt.Printf("uint16 : 0 - %d\n", math.MaxUint16)
fmt.Printf("uint32 : 0 - %d\n", math.MaxUint32)
fmt.Printf("uint64 : 0 - %d\n", uint64Max)
fmt.Printf("int8 : %d - %d\n", math.MinInt8, math.MaxInt8)
fmt.Printf("int16 : %d - %d\n", math.MinInt16, math.MaxInt16)
fmt.Printf("int32 : %d - %d\n", math.MinInt32, math.MaxInt32)
fmt.Printf("int64 : %d - %d\n", math.MinInt64, math.MaxInt64)
输出结果:
uint8 : 0 - 255
uint16 : 0 - 65535
uint32 : 0 - 4294967295
uint64 : 0 - 18446744073709551615
int8 : -128 - 127
int16 : -32768 - 32767
int32 : -2147483648 - 2147483647
int64 : -9223372036854775808 - 9223372036854775807
如果直接输出math.MaxUint64,会抛出异常:
fmt.Printf("uint64 : 0 - %d\n", math.MaxUint64)
异常信息为:
# command-line-arguments
.\int.go:16:13: constant 18446744073709551615 overflows int
原因:常量 math.MaxUint64 是无类型的,默认情况被解释为 int 类型,所以需要显示地分配uint64类型,以下为math包中 const.go 文件中定义的常量:
const (
MaxInt8 = 1<<7 - 1
MinInt8 = -1 << 7
MaxInt16 = 1<<15 - 1
MinInt16 = -1 << 15
MaxInt32 = 1<<31 - 1
MinInt32 = -1 << 31
MaxInt64 = 1<<63 - 1
MinInt64 = -1 << 63
MaxUint8 = 1<<8 - 1
MaxUint16 = 1<<16 - 1
MaxUint32 = 1<<32 - 1
MaxUint64 = 1<<64 - 1
)
4、 [m]*[n]数据类型
此写法是一个数组,一个数组指针类型的数组,可以看成是 [m]*[(n]数据类型),
package main
import "fmt"
func main() {
var arena [3]*[2]int
fmt.Printf("变量arena数据类型:%T\n",arena)
fmt.Println("arena初始值:", arena)
var a *[2]int = new([2]int)
a[0] = 3
a[1] = 5
arena[0] = a
fmt.Println("arena = ", arena)
fmt.Println("arena[0] = ", arena[0])
fmt.Println("arena[0][0] = ", arena[0][0])
}
输出结果
变量arena数据类型:[3]*[2]int
arena初始值: [<nil> <nil> <nil>]
arena = [0xc00000a0b0 <nil> <nil>]
arena[0] = &[3 5]
arena[0][0] = 3
5、Windows环境编译linux set GOOS=linux无效
如下所示, set GOOS=linux并没有设置成功
PS F:\Golang> set GOOS=linux
PS F:\Golang> go env
set GO111MODULE=on
set GOARCH=amd64
# 忽略一些参数......
set GOOS=windows
set GOPATH=F:\Golang
# 忽略一些参数......
原因:当前是在windows的PowerShell中执行的,set命令并不起使用
解决:新建一个cmd终端窗囗
验证
PS F:\Golang> set GOOS=linux
PS F:\Golang> go env
set GO111MODULE=on
set GOARCH=amd64
# 忽略一些参数......
set GOOS=linux
set GOPATH=F:\Golang
# 忽略一些参数......
6、位数不够前面补0
str := fmt.Sprintf("%08d", 19)
fmt.Println(str)
str = fmt.Sprintf("%08d", 219)
fmt.Println(str)
str = fmt.Sprintf("%08d", 3219)
fmt.Println(str)
输出结果:
00000019
00000219
00003219