golang中的variable和data types
https://golangbyexample.com/variables-in-golang-complete-guide/#What_is_variable
问题
go里面的string为什么是不可变类型的
go里面 1 && 1 语法为什么错误了?
variables
declare variables
var var_name var_type
没有初始赋值的会使用the default value of that type which is also known as zero value of that type
var <variable_name> <type> = <value>
:= operator
data types
import (
“fmt”
“reflect”
“unsafe”
)
获取大小和类型:unsafe.SizeOf 和 reflect.TypeOf
不过从这两个方法的命名上我吐血了,还是太年轻了啊
一、basic types
整数
有符号整型
int
int8
int16
int32
int64
无符号整型
uint
uint8
uint16
uint32
uint64
floats
float32
float64
float64 is the default float type. When you initialize a variable with a decimal value and don’t specify the float type, the default type inferred will be float64.
Complex Numbers
complex64
complex128
Byte
byte是int8的别名
go里面没有char
var b byte = ‘a’
rune
int32
string
string is a read only slice of bytes in golang.
String can be initialized in two ways
s1 := “this is a string”
s2 := `this is a string`
Booleans
var b bool = true
var b2 bool = false
二、composite types(复合类型)
Arrays
参考图表