静态类型语言:
语言层面的并发
强大的标准库
包管理,大部分包都在GitHub上
无泛化类型
所有Exception都用Error来处理
对C的降级处理,并发无缝,没有C降级到asm那么完美
package main //程序的包名
import "fmt" //导包
//main函数
func main() { //函数的{一定是与函数名在同一行,不然会报错
//golang中的表达式,加“;”和不加都可以,建议不加
fmt.Println("hello Go!")
}
//声明全局变量 方法一、方法二、方法三是可以的
//方法四之恶能够用在函数体内来声明
func main() {
//方法一
var a int
fmt.Println(a)
fmt.Printf("type of b = %T\n", a)
//方法二
var b int = 100
fmt.Println(b)
fmt.Printf("type of b = %T\n", b)
//方法三
var c = 100
fmt.Println(c)
fmt.Printf("type of b = %T\n", c)
//方法四
e := 100
fmt.Println(e)
fmt.Printf("type of b = %T\n", e)
}
func main() {
var xx, yy int = 100, 200
fmt.Println("xx = ", xx, " , yy = ", yy)
var kk, ll = 100, "Aceld"
fmt.Println("kk = ", kk, " , ll = ", ll)
var (
vv int = 100
jj bool = true
)
fmt.Println("vv = ", vv, " , jj =", jj)
}
// const来定义枚举类型
const (
//可以在const()添加一个关键字iota,每行的iota都会累加1,第一行的iota的默认值是0
BEIJING = 10 * iota
SHANGHAI
SHENZHEN
)
const (
a, b = iota + 1, iota + 2
c, d
e, f
g, h
i, k
)
func main() {
//常量(只读)
const length int = 10
fmt.Println("length = ", length)
}
func foo1(a string, b int) int {
fmt.Println("a = ", a)
fmt.Println("b = ", b)
c := 100
return c
}
func foo2(a string, b int) (int, int) {
fmt.Println("a = ", a)
fmt.Println("b = ", b)
return 666, 777
}
func foo3(a string, b int) (r1 int, r2 int) {
fmt.Println("a = ", a)
fmt.Println("b = ", b)
r1 = 1000
r2 = 2000
return
}
func foo4(a string , b int) (r1 , r2 int){
fmt.Println("a = ", a)
fmt.Println("b = ", b)
r1 = 1000
r2 = 2000
return
}
func main() {
c := foo1("abc", 555)
fmt.Println("c = ", c)
ret1, ret2 := foo2("haha", 999)
fmt.Println("ret1 = ", ret1)
fmt.Println("ret2 = ", ret2)
ret1, ret2 = foo3("foo3", 333)
fmt.Println("ret1 = ", ret1)
fmt.Println("ret2 = ", ret2)
}
直接在当前文件下定义init函数,在执行这个文件的其他函数之前会先执行init函数
defer关键字主要表示语句在程序运行结束之前运行
defer语句的运行顺序为栈顺序,先进的后出,后进的先出
func main() {
defer func2()
defer func3()
defer func1()
}
func func1() {
fmt.Println("A")
}
func func2() {
fmt.Println("B")
}
func func3() {
fmt.Println("C")
}
声明数组的方式:
slice(动态数组)
slice声明方式:
func main() {
//方法一
slice1 := []int{1, 2, 3}
fmt.Println("%v", slice1)
//方法二
var slice2 []int
slice2 = append(slice2, 111, 222, 2333)
fmt.Println("%v", slice2)
//方法三
var slice3 []int = make([]int, 3)
fmt.Println("%v", slice3)
//声明slice4是一个切片,同时给slice分配空间,3个空间,初始化值是0,通过:=推导出slice是一个切片
slice4 := make([]int, 3)
fmt.Println("%v", slice4)
}
func main() {
//声明myMap1是一种map类型 key是string,value是string
var myMap1 map[string]string
if myMap1 == nil {
fmt.Println("myMap1 是一个空map")
}
//在使用map前,需要先用make给map分配数据空间
myMap1 = make(map[string]string, 10)
myMap1["one"] = "java"
myMap1["two"] = "c++"
myMap1["three"] = "python"
fmt.Println(myMap1)
//第二种声明方式
myMap2 := make(map[int]string)
myMap2[1] = "java"
myMap2[2] = "c++"
myMap2[3] = "python"
fmt.Println(myMap2)
myMap3 := map[string]string{
"one": "php",
"two": "c++",
"three": "python",
}
fmt.Println(myMap3)
}
使用方式:
func printMap(cityMap map[string]string) {
for key, value := range cityMap {
fmt.Println("key =", key)
fmt.Println("value = ", value)
}
}
func ChangeValue(cityMap map[string]string) {
cityMap["England"] = "london"
}
func main() {
cityMap := make(map[string]string)
//添加
cityMap["China"] = "Beijing"
cityMap["Japan"] = "Tokyo"
cityMap["USA"] = "NewYork"
//遍历
printMap(cityMap)
//删除
delete(cityMap, "China")
//修改
cityMap["USA"] = "DC"
ChangeValue(cityMap)
fmt.Println("--------")
printMap(cityMap)
}
type Hero struct {
Name string
Ad int
Level int
}
func (this *Hero) show() {
fmt.Println("Name = ", this.Name)
fmt.Println("Ad = ", this.Ad)
fmt.Println("Level = ", this.Level)
}
func (this *Hero) GetName() {
fmt.Println("Name = ", this.Name)
}
func (this *Hero) SetName(newName string) {
this.Name = newName
}
func main() {
//创建一个对象
hero := Hero{Name: "zhang3", Ad: 100, Level: 1}
hero.show()
hero.SetName("li4")
hero.GetName()
}