Article Directory
1. Go to the official website to download
- Official website https://golang.google.cn/doc/install?download=go1.16.windows-amd64.msi
- Configure the operating environment of the go language, which contains more content and is no longer burdensome.
- If you use vs core to compile, you need to install the corresponding plug-in.
2. Run
Enter the following code
package main
import "fmt"
func main() {
fmt.Println("Hello world")
}
Run go build
, then the corresponding exe file will be generated, run the file, the generation is successful.
2.2 If you want to rename the generated file
go guild -o yourname.exe
2.3 The execution is two steps into one step (that is, execute the buid first, and then put the generated exe file in the bin directory)
go install
2.4 Cross-platform compilation
Different environments have different commands, so let's talk about it later.
3. Basic usage
3.1 Directory structure
// 这个包是属于哪个包,是main的话,可以直接执行。
package main
// 导入包,要使用双引号
import "fmt"
// 函数外只能声明变量,不能写语句
func main() {
fmt.Println("Hello world")
}
3.2 Variable declaration
package main
// 声明变量
import "fmt"
// var name string
// var age int
// var isOk bool
// 批量的声明变量
// GO 中提倡使用驼峰式的命名规则
var(
name string
age int //
isOk bool // false
)
func main() {
name="理想"
age =16
isOk = true
// Go 语言中非全局声明必须使用,不使用就编译过去
fmt.Print(isOk)
fmt.Println(name)
fmt.Printf("age:%d",age) // 使用占位符打印
}
3.3 Declaration of different variables
var s1="你好" // 当编译器能推断的时候,可以不需要指定类型。
s2:="哈哈" // 简短的变量声明,只能在函数内部使用
x,_:=foo() //匿名变量 有一个变量,但是你不想用,要使用——可以使用_
Note: Only when public variables are declared, they can be declared but not used. When local variables are declared, they cannot be compiled.
3.4 Constants and iota
iota stands for constant counter
// 常量定义以后,不能修改,在程序运行期间,不能改变。
const s3=3.1415926
// 批量声明常量
const (
STATUS_OK=200
NOTEFOUNT=404
)
const(
n1=100
n2
n3 // 不声明的话,默认都和上一行一样。
)
// iota
const(
a1=iota // a1=0
a2 // 1
a3 // 2
a4 // 3
)
iota
// iota
const(
c1=iota // c1=0
c2=100 // c2=100
c3=iota // c3=2 在同一const里,iota 出现,一行累加一次
c4 // c4=3
)
Basic use of Iota
// 定义数量级
const (
_=iota
KB=1<<(10*iota) // 左移10位
MB=1<<(10*iota) // 再左移10位
GB=1<<(10*iota) // 再左移10位
TB=1<<(10*iota) // 再左移10位
PB=1<<(10*iota) // 再左移10位
)
3.5 Floating point
func main() {
// 整型
var a int =5
fmt.Printf("%d\n",a)
// 浮点数
var b float64 =4.5
fmt.Printf("%f\n",b)
// 查看类型
f1:=1.23456
fmt.Printf("%T\n",f1)
}
3.6 Boolean
func main() {
b1:=true
var b2 bool
fmt.Printf("%v\n",b1)
fmt.Printf("%T\n,%v\n",b2,b2)
}
Output result
true
bool
false
3.7 String
The role of backticks: able to print out the original content unchanged
func main() {
// 字符串下,反斜线是原样的输出
a:=`
竹杖芒鞋轻胜马
一蓑烟雨任平生
`
fmt.Println(a)
}
The direct string cannot be changed, it can be changed into a slice, and a certain part of the string can be changed
func main() {
s1 := "白萝卜"
s2 := []rune(s1) // 强制转换成切片
s2[0] = '红' // 应该改成字符型,不应该是双引号
fmt.Println(string(s2))
}
3.8 Process control
Judgment statement
func main() {
// 单个语句的判断
age := 15
if age > 18 {
fmt.Println("澳门首家线上赌场开业了")
} else {
fmt.Println("该写暑假作业了")
}
// 多个条件判断
if age > 35 {
fmt.Println("人到中年")
} else if age > 18 {
fmt.Println("青年")
} else {
fmt.Println("好好学习!")
}
}
loop statement
func main() {
// 基本操作
for i := 0; i < 10; i++ {
fmt.Println(i)
}
// 变种1 (省略初试语句)
var a = 1
for ; a < 10; a++ {
fmt.Println(a)
}
// 键值循环
s := "Hellow 你好"
for i, v := range s {
fmt.Printf("%d,%c\n", i, v)
}
}
continue and break statements
func main() {
// 变种1 (省略初试语句)
var a = 1
for ; a < 10; a++ {
if a==3 {
continue // 调出
}
if a == 5 {
fmt.Println("over")
break // 调出for 循环
}
fmt.Println(a)
}
}
switch
swithch is equivalent to a variant of else if
func main() {
// 变种1 (省略初试语句)
n := 3
if n == 1 {
fmt.Println("大拇指")
} else if n == 2 {
fmt.Println("食指")
} else if n == 3 {
fmt.Println("中指")
} else if n == 4 {
fmt.Println("无名指")
} else if n == 5 {
fmt.Println("小拇指")
} else {
fmt.Println("无效的数字")
}
// 如果 switch 简化上面的代码
switch n {
case 1:
fmt.Println("大拇指")
case 2:
fmt.Println("食指")
default:
fmt.Println("无效的数字")
}
}
3.9 Call up the two-layer loop
You can set a flag outside, or you can use it goto + lable
, but it is recommended to use the method of setting variables outside.
func main() {
// 变种1 (省略初试语句)
var flag = false
for i := 0; i < 10; i++ {
for j := 0; j < 20; j++ {
if j == 10 {
flag = true // 调出内循环
break
}
fmt.Println(j)
}
if flag {
break // 调出外循环
}
}
}
3.10 Operator
The length of the array is part of the array type, which means that [2]int and [5]int are not the same type
var a1 [3]int
var a2 [4]int
a1=a2 // 这样是错误的
3.11 Initialization and traversal of array
func main() {
// 如果不初始化,数组就是0
var a1 [3]int
// 初始化方法1
a1 = [3]int{
1, 2, 3}
fmt.Println(a1)
// 初始化方法2
a2 := [...]int{
0, 1, 2, 3, 4} // 使用 ... 可以自动推算出来后边的数
fmt.Println(a2)
// 初始化方法3
a3 := [5]int{
0, 1}
fmt.Println(a3)
// 初始化方法4 (根据索引来初始化)
a4 := [5]int{
0: 4, 4: 2}
fmt.Println(a4)
// 数组的遍历 (使用 foreach)
citys := [...]string{
"北京", "上海", "深圳"}
for i := 0; i < len(citys); i++ {
fmt.Println(citys[i])
}
// 数组的遍历 (for range 遍历)
for i, v := range citys {
fmt.Println(i, v)
}
// 多维数组 [[1,2][3,4][5,6]]
var a5 [3][2]int
a5 = [3][2]int{
[2]int{
1, 2},
[2]int{
3, 4},
[2]int{
5, 6},
}
fmt.Println(a5)
// 数组是值类型的
b1 := [...]int{
1, 2, 3}
b2 := b1
b2[0] = 100
fmt.Println(b1)
fmt.Println(b2)
}