Home > Article > Backend Development > An article to help you understand the basic data types of Go language
##iota is in Go A constant counter can only be used together with a constant (const).
Let’s first understand this passage.
iota在const关键字出现时将被重置为0const中每新增一行常量,iota将计数(+1)一次
Example 1:
package main import "fmt" func main() { const ( n1 = iota //在const关键字出现时将被重置为0 n2 //没写相当于写了个n2=iota,每新增一行常量iota计数(+1)一次,n2 = 1 n3 //同上 n3 = 2 n4 // 同上 n4 = 3 ) fmt.Println(n1, n2, n3, n4) }
Example 2:
What will happen if you encounter _.
package main import "fmt" func main() { const ( n1 = iota //在const关键字出现时将被重置为0 n2 // n2=1 _ //匿名变量,相当于写了个 _=iota,所以此时iota=2 n4 //n4=3 ) fmt.Println(n1, n2, n4) }
Example 3:
iota 整数,很简单了,就是像 但是在Go中,整数分为两大类,正整数和没有符号的整数。 u开头的不能存负数 Differences on different platforms. 示例: 在Go中,只有 示例: 在Go中, 无示例。 注: 终于到字符串了,在Go中,字符串是基本数据类型,在栈中存储。 字符串的值为 示例: 在Go中,字符串内存布局如下。 In fact, in Go, the essence of a string is to splice characters one by one. Sometimes we may need a long statement, which takes a lot of time For long strings, we need to use ` at this time. 示例代码.
package main
import "fmt"
func main() {
const (
a, b = iota + 1, iota + 2 //在const关键字出现时,iota=0,并且两次赋值在同一行,iota没有做+1
c, d //同理,新增一行常量,常量个数为俩,仍然是一行,所以iota=1
//c, d = iota + 1, iota + 2 同上,此时iota=1,c=2,d=3
e, f // 同理,同上,e=3,f=4
)
fmt.Println(a, b, c, d, e, f)
}
整型
1,2,3,7,11,..
这样的整型数字了。
Type
##Description
##uint8 ##Unsigned 8-bit Integer type (0 to 255)
#uint16 ##uint32
Unsigned 16-bit integer (0 to 65535)
Unsigned 32-bit integer (0 to 4294967295)
uint64
##Unsigned 64-bit Integer type (0 to 18446744073709551615)
#int8
Signed 8-bit integer (-128 to 127)
##int16 Signed 16-bit integer (-32768 to 32767)
int32 Signed 32-bit integer (-2147483648 to 2147483647)
##int64
##Signed 64-bit integer ( -9223372036854775808 to 9223372036854775807)
Type
##Description
##uint ##32-bit operating system On a 64-bit operating system it is uint32
, on a 64-bit operating system it is
uint64
int On 32-bit operating systems it is
int32
, on a 64-bit operating system it is int64
uintptr
##Unsigned integer type, used to store a pointer
package main
import "fmt"
func main() {
var a = 10 //定义一个十进制数
fmt.Printf("%T\n", a) //功能:打印变量类型,结果:默认为int类型,也叫int64
fmt.Printf("%d \n", a) //功能:十进制输出,结果:10
fmt.Printf("%b \n", a) //功能:二进制输出,结果:1010
fmt.Printf("%o \n", a) //功能:二进制输出,结果:12
var b = 0b1010011010 //定义一个二进制数1010011010,以0b开头
fmt.Printf("%d\n", b) //结果:666
var c = 077 //定义一个八进制数77
fmt.Printf("%d\n", c) //结果:63
var d = 0x42 //定义一个十六进制42
fmt.Printf("%d\n", d) //结果:66
}
浮点型
float32
和float64
,默认使用的是float64
。package main
import "fmt"
func main() {
var a = 1.21 //默认为float64
fmt.Printf("%T\n", a) //结果:float64
fmt.Printf("%f\n",a)//功能:输出浮点型数,结果:1.210000
fmt.Printf("%.1f\n",a)//功能:输出浮点型数,保留小数点一位,其他忽略,结果:1.2
fmt.Printf("%.2f\n",a)//功能:输出浮点型数,保留小数点二位,其他忽略,结果:1.21
}
布尔值
bool
类型就俩值,true
和false
。
bool
类型默认值为false
。true != 1
,在Go中,bool
类型和整型不能混用。字符串
双引号("")
中的内容。package main
import "fmt"
func main() {
var name = "hello"
var name2 = "张三"
fmt.Println(name,name2)
}
##Multi-line string
package main
import "fmt"
func main() {
var lyric = `
昨夜同门云集bai 推杯又换盏
今朝du茶凉酒寒 豪言成笑谈
半生累 尽徒然zhi 碑文完美有谁看dao
隐居山水之间 誓与浮名散
湖畔青石板上 一把油纸伞
`
fmt.Println(lyric)
}
字符串常用操作
##Method
Introduction
##len(str)
Find the length
## or fmt.Sprintf Split strings
##strings.Split
segmentation
strings.contains
Determine whether it contains
##strings.HasPrefix, strings.HasSuffix
Prefix/suffix judgment
##strings.Index(), strings.LastIndex()
The position where the substring appears
##strings. Join(a[]string, sep string) join operation
package main
func main() {
//len() 求字符串字节长度
//var name = "你好,世界"
//fmt.Println(len(name)) //因为中文是utf8,一个文字三个字节,再加一个,号,所以是13
//fmt.Sprintf 或 + 拼接字符串
//name1 := "张"
//name2 := "三"
//name :=name1 + name2 //拼接字符串
//fmt.Println(name)
//name := fmt.Sprintf("%s%s", name1, name2)//同上,不过这种更灵活,推荐
//name := fmt.Sprintf("我的姓名是:%s%s", name1, name2)//自定义拼接
//fmt.Println(name)
//strings.Split 分割字符串
//name := "张三|18|男|法外狂徒"
//info := strings.Split(name, "|")
//fmt.Println(info) //返回的是切片,[张三 18 男 法外狂徒]
//strings.contains 是否包含
//name := "张三666"
//result := strings.Contains(name, "6")
//fmt.Println(result) //返回的是bool类型,true
//strings.HasPrefix 判断前缀(是不是以什么开头)
//name := "web/student/xxx.html"
//result := strings.HasPrefix(name, "web")
//fmt.Println(result) //true
//strings.HasSuffix 判断后缀(是不是以什么结尾)
//name := "web/student/xxx.html"
//result := strings.HasSuffix(name, "html")
//fmt.Println(result)//true
//strings.Index() 返回第一个字符串出现的位置
//name := "hello world"
//index := strings.Index(name, "o")
//fmt.Println(index) //4
//strings.LastIndex() // 正向数,返回最后一个字符串出现的位置
//name := "hello world"
//index := strings.LastIndex(name, "o")
//fmt.Println(index) // 7
//strings.Join(a[]string, sep string) 字符串拼接
//info_list := []string{"张三", "男", "18"} //定义一个列表
//name := strings.Join(info_list, "|")
//fmt.Println(name) //张三|男|18
}
The above is the detailed content of An article to help you understand the basic data types of Go language. For more information, please follow other related articles on the PHP Chinese website!