Go basic data types

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:
  • Integer (int, unit)
  • Float (float32, float64)
  • Other: byte (similar unit8), rune (Int32 similar) etc.
String type
Derived type:
  • Pointer type (the Pointer)
  • Array type (Array)
  • Structure type (StructEntryTable)
  • Pipeline type (Channel)
  • Type function (Function)
  • Slice type (Slice)
  • Interface Type (Interface)
  • Map Type (Map)
4 kinds of scalar type:
  • boolean (Boolean)
  • integer (integer)
  • float (float, also called double)
  • string (string)
Three Compounds Type:
  • array (array)
  • object (the object)
  • callable (callable)
Two kinds of special types:
  • resource (resources)
  • NULL (no type)
String
Byte coding
  • Byte string using UTF-8 encoding default
  • Unicode character support
  • Only the 256-supported character set, need to explicitly specify the character code in the code  header ( "the Content-the Type: text / HTML; charset = UTF-. 8");  or modify the php.ini default character set is provided  default_charset = " utf-8 " 
  • It does not support Unicode characters
String definitions
  • Double quotes ( ""): Double quotes escape character will be replaced
  • Backticks ( `): backquote natively escape character string (eg \ n) is outputted as
  • Single quotation marks ( ''): a single character enclosed in single quotes, it is necessary to format the output,% c, otherwise the output character code number
  • Double quotes ( ""): Double quotes escape character will be replaced, you can resolve the variable, the variable is about to be enclosed in double quotes
  • Single quotation marks ( ''): Go and anti quotes like escape character is not escaped, as output
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:
  •  mb_strlen ()  : mbstring extension to be installed
  •  iconv_strlen ()  : Returns the number of characters in the string of statistics, is an integer
String concatenation
Operator +:
str := "hello, " +
"world"
  • Between the string plus "+" stitch. If the wrap, + to put at the end of the current line, can not be put at the beginning of the next line
  • Using this splicing, which strings are immutable, each operation will produce a new, temporary string to GC additional burden, so the relatively poor performance
Operator:
$str = "hello, " . "world";
// 也可以写成:
$str = "hello, " . "world";
  • 字符串之间用句点 “.”拼接,换行对句点的位置没有要求
fmt.Sprintf():
fmt.Sprintf("%d:%s", 2018, "年")
  • 内部使用 []byte 实现,不会产生临时的字符串
  • 内部逻辑复杂,还用到了interface,性能一般
strings.Join()
strings.Join([]string{"hello", "world"}, ", ")
  • Join会先根据字符串数组的内容,计算出一个拼接之后的长度,然后申请对应大小的内存,一个一个字符串填入
  • 已有一个数组的情况下,这种拼接方式的效率很高,但如果没有,去构造这个数据的代价也不小。
bytes.Buffer——优先推荐
var buffer bytes.Buffer
buffer.WriteString("hello")
buffer.WriteString(", ")
buffer.WriteString("world")
 
fmt.Print(buffer.String())
  • 使用这种拼接方式,可以把字符串当成可变字符使用,对内存的增长也有优化
  • 如果能预估字符串的长度,还可以用 buffer.Grow() 接口来设置 capacity。
strings.Builder——推荐
var b1 strings.Builder
b1.WriteString("ABC")
b1.WriteString("DEF")
 
fmt.Print(b1.String())
  • 内部通过 slice 来保存和管理内容
  • 提供了 Grow() 来支持预定义容量,这样可以避免扩容而创建新的 slice
  • 非线程安全,性能上和 bytes.Buffer 相差无几
数组
声明数组
  • 数组元素必须类型唯一,要么都是字符串,要么都是数字类型。如果想让数组元素类型为任意类型,可以使用空接口interface{}作为类型,当使用值时必须先做一个类型判断。
  • 声明时需要确定长度,如果采用不定长数组的方式声明,在初始化时会自动确定好数组的长度。
① 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中的引用传递使用 & 符号。
切片
定义
  • 切片是引用(对底层数组一个连续片段的引用),不需要占用额外的内存,比数组效率高
  • 一个切片未初始化前默认为nil,长度为0
  • 切片的长度可变,可以把切片看成一个长度可变的数组
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)
字典
定义
  • 一种元素对的无序集合,一组称为元素value,另一组为唯一键索引key
  • 未初始化 map 的值为 nil。map 是引用类型
  • map 可以动态增长,声明时不需要定义长度
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 的区别绝不止上面列出的这些,对比新旧两种语言的差别意义也不是很大,但对我来说,通过梳理可以达到温故知新的目的。 

Guess you like

Origin www.cnblogs.com/sunshineliulu/p/11616495.html