This is the first Go study notes, primarily Go basic data types of learning are summarized.
Since I have been using PHP language development, found that some knowledge often confused, ordered to Go and PHP were compared (limited to basic data types of the two) in the process of learning Go's.
|
Go
|
PHP
|
|
Basic data types
|
Boolean
Digital Type:
String type
Derived type:
|
4 kinds of scalar type:
Three Compounds Type:
Two kinds of special types:
|
|
String
|
Byte coding
|
|
|
String definitions
|
|
|
|
String length
|
len () : Get the UTF-8 character encoding for each length and , instead of directly to the number of characters. Such as len ( "hello, world") the result is 12 instead of 8 |
strlen () : Go and the len () is similar to the string to obtain the number of bytes occupied by | |
utf8.RuneCountInString () : Gets a string of real length, the above utf8.RuneCountInString ( "hello, world") the result is 8. Note: the need to import "unicode / utf8" |
Get the length of the string actually can use the following two functions:
|
||
String concatenation
|
Operator +:
str := "hello, " + "world"
|
Operator:
$str = "hello, " . "world"; // 也可以写成:
|
|
fmt.Sprintf():
fmt.Sprintf("%d:%s", 2018, "年")
|
|||
strings.Join()
strings.Join([]string{"hello", "world"}, ", ")
|
|||
bytes.Buffer——优先推荐
var buffer bytes.Buffer buffer.WriteString("hello") buffer.WriteString(", ") buffer.WriteString("world") fmt.Print(buffer.String())
|
|||
strings.Builder——推荐
var b1 strings.Builder b1.WriteString("ABC") b1.WriteString("DEF") fmt.Print(b1.String())
|
|||
数组
|
声明数组
|
① var arr [2]int //var 数组名称 [数组长度]数组元素类型
② var arr = [2]int{1,2} //var 数组名称 = [数组长度]数组元素类型{元素1,元素2,...}
③ var arr = [5]string{3: "ab", 4: "cd"} //指定索引位置
④ var arr = [...]int{1,2} //var 数组名称 = [...]数组元素类型{元素1,元素2,...},不定长数组声明方式
⑤ new():创建的是数组引针,eg. var arr1 = new([5]int)
|
$arr = ['a', 'b', 'c', 123];
如果是 Go,会报错:
# command-line-arguments ./arr.go:6:34: cannot use 123 (type int) as type string in array or slice literal |
值传递和
引用传递
|
var arr1 = new([5]int) arr := arr1 arr1[2] = 100 fmt.Println(arr1[2], arr[2]) var arr2 [5]int newarr := arr2 arr2[2] = 100 fmt.Println(arr2[2], newarr[2]) 程序输出: 100 100 100 0
new([5]int)创建的是数组指针,arr和arr1指向同一地址,故而修改arr1时arr同样也生效;
而newarr是由arr2值传递(拷贝),故而修改任何一个都不会改变另一个的值。
|
达到与左侧相同的效果,PHP 代码如下:
$arr1 = [5, 10, 0, 20, 25]; $arr = &$arr1; //引用传递 $newArr = $arr1; //值传递 $arr1[2] = 100; echo $arr1[2], $arr[2]; echo $arr1[2], $newArr[2]; 程序输出: 100 100 100 0
PHP中的引用传递使用 & 符号。
|
|
切片
|
定义
|
|
PHP中没有切片这个概念,但是在数组函数中有个 array_slice() 函数,可以根据 offset 和 length 返回指定的数组中的一段序列。与 Go 的切片相似。
$input = array("a", "b", "c", "d", "e"); $output = array_slice($input, 0, 3); // returns "a", "b", and "c" |
声明
|
① 切分数组
var arr = [5]int{1,2,3,4,5} //数组
var s []type = arr[start:end:max] //切片(包含数组start到end-1之间的元素),end-start表示长度,max-start表示容量
② 初始化
var s = []int{2, 3, 5, 7, 11} //创建了一个长度为 5 的数组并且创建了一个相关切片。
③ 切片重组
var s []type = make([]type, len, cap) //len 是数组的长度也是切片的初始长度,cap是容量,可选参数
简写为slice := make([]type, len)
|
||
字典
|
定义
|
|
PHP的关联数组类似于 Go 的 Map。 |
声明
|
① var mapName map[keyType]valueType
② var mapName map[keyType]valueType{k1:v1, k2:v2, ...}
③ var mapName = make(map[keyType]valueType, cap) //cap是可选参数,表示容量
|
其实 Go 的基本数据类型的知识点以及它和 PHP 的区别绝不止上面列出的这些,对比新旧两种语言的差别意义也不是很大,但对我来说,通过梳理可以达到温故知新的目的。