Go语言基础_数据类型、基本语法篇

Go语言基础


Go的数据类型

基本数据类型

  • 布尔型bool
  • 字符型string
  • 整型int int8 int16 int32 int64
  • unit unit8 unit16 uint32 unit64 uintptr
  • 字节型byte
  • rune
  • float32 float64
  • complex64 complex128

与其他语言的差异

  • Go不支持隐式类型转换
  • 别名和原有类型也不能进行隐式类型转换

示例

错误の示例1

Go语言基础_数据类型、基本语法篇_第1张图片

错误の示例2

Go语言基础_数据类型、基本语法篇_第2张图片

类型的预定义值

在math包下,有int、float、unit,直接使用即可

Go语言基础_数据类型、基本语法篇_第3张图片


指针类型

支持使用指针直接访问内存空间

与其他语言的差异

  • 不支持指针运算
  • string是值类型,默认的初始化值是空字符串,不是nil

示例

使用示例
func main() {
	a:=1
	aPtr:=&a
	fmt.Println(a,aPtr)
	//%T获取变量类型
	fmt.Printf("%T %T",a,aPtr )
}
######################################
1 0xc000128058
int *int
Process finished with the exit code 0
差异の示例1

Go语言基础_数据类型、基本语法篇_第4张图片

差异の示例2
func main() {
	var s string
	fmt.Println("*"+s+"*")
	fmt.Println(len(s))
}
######################################
**
0


数组和切片


Map

与其他语言的差异

  • 获取不存在的key不会报空指针(参考java),会根据value的类型返回默认值,比如int是0,string是 nil。此时不能简单的通过nil判断key是否存在,但是可以主动判断key是否存在
  • map的value可以是一个方法
  • 与go的dock type接口方式一起,可以实现单一方法对象的工厂模式

示例

map的声明
	//map[key type]value type
	map1:=map[string]int{"one":1,"two":2}
	map2:=map[string]int{}
	map1["one"]=11
	//make(type,capacity),map跟切片都是自增长度的存储,如果容量不够,就会分配新的内存空间然后把原来的值进行拷贝。如果我们提前就知道了map的容量,那么就可以使用map初始化,避免因为频繁扩容带来的开销,提高性能
	map3:=make(map[string]int,10)
	fmt.Println(map1)
	fmt.Println(map2)
	fmt.Println(map3)
map的遍历
func TestRangeMap(t *testing.T){
	map1:=map[string]int{"one":1,"two":2,"three":3}
	for k,v:=range map1{
		t.Log(k,v)
	}
}
差异の示例1
func TestMap(t *testing.T){
	map1:=map[string]int{"one":1,"two":2}
	if  v,ok:=map1["three"];ok{
		fmt.Println(v)
	}else{
		fmt.Println("three not exist~")
	}
}
######################################
=== RUN   TestMap
three not exist~
map[]
map[]
--- PASS: TestMap (0.00s)
PASS
差异の示例2
func TestFunValueMap(t *testing.T){
	 funcMap:=map[int]func(v int)int{}
	 funcMap[1]=func(v int)int{return v}
	 funcMap[2]=func(v int)int{return v*v}
	 funcMap[3]=func(v int)int{return v*v*v}
	 t.Log(funcMap[1](2),funcMap[2](2),funcMap[3](2))
}
######################################
=== RUN   TestFunValueMap
    operator_test.go:56: 2 4 8
--- PASS: TestFunValueMap (0.00s)
PASS

Set

与其他语言的差异

  • go的内置集合中没有set,可以使用map来自己实现:map[type]bool。实际上java底层的set也是使用 map来实现的
func TestSet(t *testing.T){
	mySet:=map[int]bool{}
	mySet[1]=true
	n:=1
	//判断元素是否存在
	if mySet[n] {
		t.Logf("%d is exist",n)
	}else {
		t.Logf("%d is not exist",n)
	}
	//查看set元素个数
	t.Log(len(mySet))
	//删除key
	delete(mySet,1)
}

字符串

与其他语言的差异

  • string是数据类型 ,不是引用或者指针类型
  • string是只读的 byte slice,len函数可以查看它所包含的byte数
  • string的byte数组可以存放任何二进制数据

Unicode UTF8

  • unicode是一种字符集
  • UTF8是unicode的存储实现
  • rune能够取出字符串里的unicode
示例
func TestChar(t *testing.T){
	s:="中"
	//byte数
	t.Log(len(s))
	c:=[]rune(s)
	t.Logf("中 unicode %x",c[0])
	t.Logf("中 UTF8 %x",s)
}
######################################
=== RUN   TestChar
    operator_test.go:78: 3
    operator_test.go:80: 中 unicode 4e2d
    operator_test.go:81: 中 UTF8 e4b8ad
--- PASS: TestChar (0.00s)
PASS

常用的字符串函数

  • strings包 https://golang.org/pkg/strings/
  • strconv包 https://golang.org/pkg/strconv/

Go的基本语法

运算符

与其他语言的差异

  • 用 ==比较数组,相同维度并且还有相同个数元素的数组才可以比较。每个元素都相同才相等
  • &^按位值零,只要右边操作数设为1的二进制位,都会把左边的二进制位清0,如过右边是0,那么左边是什么结果就是什么

​ 1&^0 --1

​ 1&^1 --0

​ 0&^1 --0

​ 0&^0 --0

示例

使用示例
func TestOperator(t *testing.T){
	a:=[...]int{1,2,3,5}
	b:=[...]int{1,2,3,4}
	c:=[...]int{1,2,3,5}
	t.Log(a==b)
	t.Log(a==c)
}
######################################
=== RUN   TestOperator
    operator_test.go:9: false
    operator_test.go:10: true
--- PASS: TestOperator (0.00s)
PASS
差异の示例1

Go语言基础_数据类型、基本语法篇_第5张图片


循环

与其他语言的差异

  • go仅支持关键字for
  • for循环的三个条件都是可以省略的,如果三个都省略就是while
  • if语句支持变量赋值
  • switch:
    • 条件表达式不限制为常数或者整数
    • 单个case中 ,可以出现多个结果,逗号分割
    • 不需要用break来明确退出
    • 可以不设定switch之后的条件表达式,此时与多个if…else…逻辑等同

示例

差异の示例1
func TestFor(t *testing.T) {
	n := 0
	//while n<5
	for n < 5 {
		t.Log(n)
		n++
	}
}
######################################
=== RUN   TestFor
    operator_test.go:17: 0
    operator_test.go:17: 1
    operator_test.go:17: 2
    operator_test.go:17: 3
    operator_test.go:17: 4
--- PASS: TestFor (0.00s)
PASS
差异の示例2
func TestIf(t *testing.T) {
	if v, err := someFunc(); err == nil {
		t.Log("no err,value:", v)
	} else {
		t.Log("err:", err)
	}
}
差异の示例3
func TestIf(t *testing.T) {
	if v, err := someFunc(); err == nil {
		t.Log("no err,value:", v)
	} else {
		t.Log("err:", err)
	}
}

你可能感兴趣的:(go,golang,开发语言,后端)