4.1 基本元素
包
一个包包含 $n\in Z^+$ 个 .go
文件。文件通过首行 package <package_name>
声明包名。
引入包
import "fmt"
// 或者
import "fmt"; import "os"
// 或者
import (
"fmt"
"os"
)
可见性
大写开头的名称可导出到包外,小写开头的名称只能包内使用。
函数
示例:
func myFunc(in1 bool, in2 bool, in3 bool) (out1 bool, out2 bool, out3 bool) {
return in1 != in2, in2 != in3, in3 != in1
}
注释
函数注释必须以函数名开始。注释风格与 C 相同。
类型
Go 有基本类型、复合类型、接口类型。
基本类型:int、float、bool、string。有默认值
复合类型:struct、array、slice、map、channel。无默认值(nil
)
接口类型:interface
编译器支持类型推断。
类型转换
将类型以函数形式调用实现转换。
a := 5.0
b := int(a)
4.2 常量
// 续行
const Ln2= 0.693147180559945309417232121458\
176568075500134360255254120680009
// 精确实数
const Log2E= 1/Ln2 // this is a precise reciprocal
// 浮点数
const Billion = 1e9 // float constant
// 不会溢出
const hardEight = (1 << 100) >> 97
// 并行赋值
const beef, two, c = “meat”, 2, “veg”
// 枚举
const (
Unknown = 0
Female = 1
Male = 2
)
// 自增枚举
const (
a = iota // 0
b = iota // 1
c = iota // 2
)
// 省略形式
const (
a = iota
b
c
)
以反射库为例:
// A Kind represents the specific kind of type that a Type represents.
// The zero Kind is not a valid kind.
type Kind uint
const (
Invalid Kind = iota
Bool
Int
Int8
Int16
Int32
Int64
Uint
Uint8
Uint16
Uint32
Uint64
Uintptr
Float32
Float64
Complex64
Complex128
Array
Chan
Func
Interface
Map
Ptr
Slice
String
Struct
UnsafePointer
)
4.3 变量
声明
var a, b, c int
var a, b *int
var a int
var b bool
var str string
var (
a int
b bool
str string
)
值类型和引用类型
基本类型都属于值类型。复合类型的 array 和 struct 属于值类型。其特点是直接指向内存值。
pointer、slice、map、channel 属于引用类型。
init 函数
- 不能够被人为调用
- 在每个包完成初始化后自动执行
- 执行优先级比 main 函数高
- 按照包的依赖关系顺序执行
4.4 基本类型和运算符
基本类型和运算符 - the-way-to-go_ZH_CN (gitbook.io)
- bool
- 不支持异或
^
,用!=
代替。
- 不支持异或
- 没有 float,double,只有 float32,float 64
- int:
- int/uint
- int8/uint8…int64/uint64
- complex64/128 复数:
10+2i
- 从 float32 创建:
c = complex (re, im)
- 获得实部和虚部:real (c) 和 imag (c)
- 从 float32 创建:
格式化符号
符号 | 用途 |
---|---|
%d | 整数 |
%x %X | 十六进制整数 |
%g | 浮点数 |
%f | 浮点数 |
%e | 科学记数法 |
%v | 复数 |
%b | 位 |
%c | 字符 |
$U | U+hhhh 字符 |
类型别名
type <new_name> <name>
新类型不会拥有原类型所附带的方法
byte 是 uint8 的别名
十六进制字符:'\x41'
八进制字符:\377
Unicode 字符:\uxxxx
, \Uxxxxxxxx
4.5 串
转义
如:\n
反引号原样输出:
`this is\n not escaped.`
常用操作
操作 | 作用 |
---|---|
len (str) | 计算字节长度 |
str [i] | 取出字节 |
s1+s2 | 连接 |
strings.Join () | 连接 |
utf8.RuneCountInString (str) | 计算字符长度 |
strings 和 strconv 包
strings
.HasPrefix (s, pre) // 前缀判断
.HasSuffix (s, suf) // 后缀判断
.Contains (s, sub) // 包含判断
.Index (s, sub)// 字串首位置
.LastIndex (s, sub)// 字串末位置
.IndexRune (s, rune)// 字符位置
.Replace (str, old, new, count)// 替换
.Count (s, sub)// 非重叠次数
.Repeat (s, n)// 重复 n 次返回
.ToLower (s)// 转小写
.ToUpper (s)// 转大写
.TrimSpace (s)// 两端剔除
.TrimLeft (s)// 左端剔除
.TrimRight (s)// 右端剔除
.Fields (s)// 空白字符分割,移除空项
.Split (s, sep)// 自定义分割
.Join (s, sep)// 分隔符连接
读取字符串:
var reader = strings.NewReader(str)
reader.Read () // 读取
reader.ReadByte ()// 读取字节
reader.ReadRune ()// 读取字符
类型转换:
strconv.Itoa (i int) // 数字转字符串
strconv.FormatFloat (f float64, fmt byte, prec int, bitSize int) // 浮点转字符串
strconv.Atoi (s string) (i int, err error) // 串转 int
strconv.ParseFloat (s string, bitSize int) (f float64, err error)// 串转 float64
时间和日期
time
包
time.Now () 当前时间
t.Day () // 时间的日部分
t.Format (layout) // 格式化时间
指针
只允许引用,不允许运算。