青训营X豆包MarsCode 技术训练营第一课 | 豆包MarsCode AI 刷题

91 阅读3分钟

一、走进Go语言基础语法

1.Go语言的特点:

(1)高性能、高并发;

(2)语法简单、学习曲线平缓;

(3)丰富的标准库;

(4)完善的工具链;

(5)静态链接;

(6)快速编译;

(7)跨平台;

(8)垃圾回收

2.目前使用Go语言的公司

字节跳动,Google,腾讯,facebook,美团,七牛云,滴滴,bilibili,百度,PingCAP

3.配置Go的开发环境

(1)GoLang:

重要网址:

go.dev/

studygolang.com/dl (中国镜像)

goproxy.cn/

(2)配置IDE(集成开发环境)

vscode或者GoLand(收费,学生期间可申请免费试用)

4.Go的基础语法 (1)输出“hello world”

package main

import "fmt"

func main(){ fmt.Println("hello world") }

代码解释:

package main表示该文件属于"main"包,是“入口包”

import "fmt"导入“fmt"包,主要用于在屏幕上输出输入字符串,格式化字符串

5.变量

package main

import ( "fmt" "math" )

func main() {

var a = "initial"

var b, c int = 1, 2

var d = true

var e float64

f := float32(e)

g := a + "foo"
fmt.Println(a, b, c, d, e, f) // initial 1 2 true 0 0
fmt.Println(g)                // initialapple

const s string = "constant"
const h = 500000000
const i = 3e20 / h
fmt.Println(s, h, i, math.Sin(h), math.Sin(i))

}

(1)变量

变量在Go中是内置类型,可以直接用“+”拼接,也可以直接用“=”判断大小

变量声明方式:

(a)变量名:=值

 例如:
 f := float32(e)

(b)var 变量名=值

 例如:
 var a = "initial"
 var b, c int = 1, 2

(2)常量

常量没有确定的类型,会根据上下文自动确定类型

常量声明方式:

(a)const 变量名=值

 例如:
 const s string = "constant"
 const h = 500000000
 const i = 3e20 / h

(3)if-else语句

package main

import "fmt"

func main() {

if 7%2 == 0 {
	fmt.Println("7 is even")
} else {
	fmt.Println("7 is odd")
}

if 8%4 == 0 {
	fmt.Println("8 is divisible by 4")
}

if num := 9; num < 0 {
	fmt.Println(num, "is negative")
} else if num < 10 {
	fmt.Println(num, "has 1 digit")
} else {
	fmt.Println(num, "has multiple digits")
}

}

Go中的if-else语句与C/C++不同的地方在于Go中的if语句之后没有(),只有else之后有()。如果在编写代码过程中if语句用了(),则在执行过程中会自动删除if之后的()

例如:if 7%2 == 0 {
	fmt.Println("7 is even")
} else {
	fmt.Println("7 is odd")
}
   

if语句后边必须跟{},不能将if和else的语句写到同一行

(4)循环(没有while循环和do-while循环)

for循环语句

package main

import "fmt"

func main() {

i := 1
for {
	fmt.Println("loop")
	break
}
for j := 7; j < 9; j++ {
	fmt.Println(j)
}

for n := 0; n < 5; n++ {
	if n%2 == 0 {
		continue
	}
	fmt.Println(n)
}
for i <= 3 {
	fmt.Println(i)
	i = i + 1
}

} 可以用continue和break,for语句之后不用加()

(5)switch分支结构 package main

import ( "fmt" "time" )

func main() {

a := 2
switch a {
case 1:
	fmt.Println("one")
case 2:
	fmt.Println("two")
case 3:
	fmt.Println("three")
case 4, 5:
	fmt.Println("four or five")
default:
	fmt.Println("other")
}

t := time.Now()
switch {
case t.Hour() < 12:
	fmt.Println("It's before noon")
default:
	fmt.Println("It's after noon")
}

}

(a)switch之后的变量名不需要加()

例如:switch a{......}

(b)case语句里面可以不用加break,默认执行完一个case之后直接跳出(C/C++中需要break跳出)

例如:switch a {
case 1:
	fmt.Println("one")
case 2:
	fmt.Println("two")
case 3:
	fmt.Println("three")
case 4, 5:
	fmt.Println("four or five")
default:
	fmt.Println("other")
}

(6)数组

package main
import "fmt"
func main(){
var a [5]int
a[4]=100
fmt.Println(a[4],len(a))
b:=[5]int{1,2,3,4,5}
fmt.Println(b)
var twoD [2][3]int
for i:=0;i<2;i++{
	for j:=0;j<3;j++{
		twoD[i][j]=i+j
	}
}
fmt.Println("2d:",twoD)
}

(7)切片(长度不固定)

package main

import "fmt"

func main() {

s := make([]string, 3)
s[0] = "a"
s[1] = "b"
s[2] = "c"
fmt.Println("get:", s[2])   // c
fmt.Println("len:", len(s)) // 3

s = append(s, "d")
s = append(s, "e", "f")
fmt.Println(s) // [a b c d e f]

c := make([]string, len(s))
copy(c, s)
fmt.Println(c) // [a b c d e f]

fmt.Println(s[2:5]) // [c d e]
fmt.Println(s[:5])  // [a b c d e]
fmt.Println(s[2:])  // [c d e f]

good := []string{"g", "o", "o", "d"}
fmt.Println(good) // [g o o d]
}

注意make和append的使用方式

(8)map

package main

import "fmt"

func main() {

s := make([]string, 3)
s[0] = "a"
s[1] = "b"
s[2] = "c"
fmt.Println("get:", s[2])   // c
fmt.Println("len:", len(s)) // 3

s = append(s, "d")
s = append(s, "e", "f")
fmt.Println(s) // [a b c d e f]

c := make([]string, len(s))
copy(c, s)
fmt.Println(c) // [a b c d e f]

fmt.Println(s[2:5]) // [c d e]
fmt.Println(s[:5])  // [a b c d e]
fmt.Println(s[2:])  // [c d e f]

good := []string{"g", "o", "o", "d"}
fmt.Println(good) // [g o o d]
}