This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vim: syntax=go | |
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
func main() { | |
//拼接1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vim: syntax=go | |
package main | |
import "strconv" | |
func main(){ | |
//int to string | |
string := strconv.Itoa(int) | |
//int64→string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vim: syntax=go | |
一 数值类型 | |
数值类型指基本类型中的:整型、浮点型、复数。 | |
二 整数 | |
整数类型有无符号(如int)和带符号(如uint)两种,这两种类型的长度相同,但具体长度取决于不同编译器的实现。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vim: syntax=go | |
/** | |
值传递和引用传递 | |
不管是值传递还是引用传递,传递给函数的都是变量的副本,不同的是,值传递的是值的拷贝,引用传递的是地址的拷贝,一般来说,地址拷贝效率高,因为数据量小,而值拷贝决定拷贝的 数据大小,数据越大,效率越低。 | |
如果希望函数内的变量能修改函数外的变量,可以传入变量的地址&,函数内以指针的方式操作变量。 | |
**/ | |
// 函数声明格式: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vim: syntax=go | |
# 数组 | |
## 一 数组 | |
#### 1.1 数组的声明 | |
数组是一段固定长度的连续内存区域。数组的长度定义后不可更改,长度使用 len() 获取。 | |
```go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vim: syntax=go | |
## 一 集合map | |
#### 1.1 map的创建 | |
Go内置了map类型,map是一个无序键值对集合(也有一些书籍翻译为字典)。 | |
普通创建: | |
```go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vim: syntax=go | |
## 一 流程控制之-条件语句 | |
#### 1.1 判断语句 if | |
`if`判断示例: | |
```go | |
// 初始化与判断写在一起: if a := 10; a == 10 | |
if i == '3' { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vim: syntax=go | |
//多态 【有一个父类(有接口) / 有子类(实现了父类的全部接口方法) / 父类类型的变量(指针)指向(引用)子类的具体数据变量 / 】 | |
//本质是一个指针 | |
type AnimalIF interface { | |
Sleep() | |
GetColor() string //获取动物的演示 | |
GetType() string //获取动物的种类 | |
} | |
func aniMalSleep(animal AnimalIF) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vim: syntax=go | |
// 路径操作 | |
fmt.Println(filepath.IsAbs("./test.txt")) // false:判断是否是绝对路径 | |
fmt.Println(filepath.Abs("./test.txt")) // 转换为绝对路径 | |
// 创建目录 | |
err := os.Mkdir("./test", os.ModePerm) | |
if err != nil { | |
fmt.Println("mkdir err: ", err) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 一 反射的使用 | |
#### 1.1 反射操作简单数据类型 | |
```go | |
var num int64 = 100 | |
// 设置值:指针传递 | |
ptrValue := reflect.ValueOf(&num) | |
newValue := ptrValue.Elem() // Elem()用于获取原始值的反射对象 | |
fmt.Println("type:", newValue.Type()) // int64 |
OlderNewer