运算符
- 算术运算符
- +
- -
- *
- /
- %
- ++
- --
- 关系运算符
- ==
- !=
- >
- <
- >=
- <=
- 逻辑运算符
- &&
- ||
- !
- 位运算符
- &
- 有0得0
- |
- 有1得1
- ^
- 相同为0
- <<
- >>
- 赋值运算符
- +=
- -=
- *=
- /=
- %=
- <<=
- >>=
- &=
- |=
- ^=
- 其他运算符
- &
- *
运算符优先级
优先级 | 运算符 |
7 | ^! |
6 | */%<<>>& &^ |
5 | +-|^ |
4 | == != <= >= > |
3 | <- |
2 | && |
1 | || |
指针
- 取址符
- &
- 指针变量可以指向任何一个值的内存地址
var var_name *var-type
var ip *int
var fp *float32
- 示例
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package main import "fmt" func main(){ var a = 10 var b *int b = &a fmt.printf("a's adress %x\n",&a) //b's adress fmt.printf("b's adress %x\n",&b) //b's value fmt.printf("b's value %d\n",*b) } |
空指针
- 当一个指针被定义后却没有分配任何值
- 值为nil,指代零值或空值
1 2 |
if(ptr != nil) //ptr不是空指针 |
结构体
定义
1 2 3 4 5 |
type struct_variable_type struct { member definition; member definition; ... member definition;} |
结构体用于变量
variable_name := structure_variable_type {value1,value2,value3}
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package main import "fmt" type Books struct { title string author string subject string book_id int} func main(){ var Book1 Books var Book2 Books Book1.title = "go" Book1.author = "lif" Book1.subject = "jiaoc" Book1.book_id = 123545 Book2.title = "python" Book2.author = "wangxu" Book2.subjuect = "sss" Book2.book_id = 234555 fmt.Printf("Book1's title %s\n",Book1.title) //访问输出类似 } |
结构体用作函数参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package main import "fmt" type Books struct { title string author string subjec string book_id int} func main(){ var Book1 Books var Book2 Books Book1.title = "go" Book1.author = "lif" Book1.subjec = "jiaoc" Book1.book_id = 123545 Book2.title = "python" Book2.author = "wangxu" Book2.subjec = "sss" Book2.book_id = 234555 printBook(Book1) printBook(Book2) } func printBook(book Books){ fmt.Printf("Book's title %s\n",Book1.title) fmt.Printf("Book's author %s\n",Book1.author) fmt.Printf("Book's subject %s\n",Book1.subjec) fmt.Printf("Book's id %s\n",Book1.book_id) } |
定义结构体指针
1 2 |
var struct_pointer * Books struct_pointer = &Book1; |
Printf函数转义字符
%d | 十进制 |
%x | 十六进制 |
%o | 八进制 |
%b | 二进制 |
%f,%g,%e | 浮点数 |
%t | 布尔型 |
%c | 字符 |
%s | 字符串 |
%q | 带引号字符串 |
%v | 内置格式的任何值 |
%T | 任何值的类型 |
%% | 百分号自身 |
Tips
- 首字母大写的结构体、方法、函数都可以被其他的包所引用
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 包管理器:各平台安装卸载相关记述09/17
- ♥ 创建型:原型模式09/25
- ♥ 连接查询-笛卡尔积 || 连接方式10/25
- ♥ WinDbg语法规则07/11
- ♥ 静态库10/02
- ♥ 网络相关11/21