随笔分类 -  golang

摘要:生成json格式字符 使用结构体生成 package main import ( "encoding/json" "fmt" ) //用于json的结构体类型成员首字母必须大写 // type Jon struct { // Name string // Subject []string // Sc 阅读全文
posted @ 2021-02-28 19:35 GPHPER 阅读(165) 评论(0) 推荐(0)
摘要:字符串操作 package main import ( "fmt" "strings" ) func main() { str := "hello world" //contains 是否包含指定字符串 fmt.Println(strings.Contains(str, "hello")) //Ji 阅读全文
posted @ 2021-02-28 16:34 GPHPER 阅读(159) 评论(0) 推荐(0)
摘要:自定义异常的两种方式 package main import ( "errors" "fmt" ) func main() { //使用fmt.Errorf err1 := fmt.Errorf("%s", "this is normal error") fmt.Println(err1) //使用 阅读全文
posted @ 2021-02-28 16:14 GPHPER 阅读(103) 评论(0) 推荐(0)
摘要:接口 定义及使用 package main import ( "fmt" ) //定义一个接口 type Human interface { sayHello() } type Student struct { name string age int } type Teacher struct { 阅读全文
posted @ 2021-02-28 15:33 GPHPER 阅读(147) 评论(0) 推荐(0)
摘要:继承 匿名字段(可以是任意类型) package main import ( "fmt" ) type Person struct { id int username string sex byte } type Student struct { Person //匿名字段继承了Person的成员 阅读全文
posted @ 2021-02-28 11:49 GPHPER 阅读(123) 评论(0) 推荐(0)
摘要:定义 type 变量名 struct{ 元素1名称 元素1类型 元素2名称 元素2类型 } package main import ( "fmt" ) type student struct { id int name string age int addr string } func main() 阅读全文
posted @ 2021-02-28 10:54 GPHPER 阅读(164) 评论(0) 推荐(0)
摘要:map数据类型 形式如 map[keyType]valueType 类型的数据 定义 //直接定义 m2 := map[int]string{1: "hello", 2: "world"} fmt.Println("m2 = ", m2) //使用make函数定义 m1 := make(map[in 阅读全文
posted @ 2021-02-28 10:17 GPHPER 阅读(286) 评论(0) 推荐(0)
摘要:一维数组赋值 package main import ( "fmt" ) func main() { //全部赋值 var a [5]int = [5]int{1, 2, 3, 4, 5} fmt.Println("a = ", a) //部分赋值 b := [...]int{3, 4, 5} fm 阅读全文
posted @ 2021-02-27 17:10 GPHPER 阅读(139) 评论(0) 推荐(0)
摘要:不定参数类型函数定义 【args ...type】 package main import "fmt" func Func01(a byte, args ...int) { fmt.Printf("len(arg) is %d\n", len(args)) fmt.Printf("a is %c", 阅读全文
posted @ 2021-02-27 13:37 GPHPER 阅读(93) 评论(0) 推荐(0)
摘要:获取命令行参数 【开发命令行工具推荐使用 https://github.com/spf13/cobra】 package main import "fmt" import "os" func main() { //获取参数关键代码 list := os.Args for k, v := range 阅读全文
posted @ 2021-02-27 11:00 GPHPER 阅读(98) 评论(0) 推荐(0)
摘要:观看B站李文周老师的视频学习golang整理的笔记 变量 定义 var 变量名 变量类型 多个 var( a int b int ) 自动识别变量类型运算符“:=” a := 10 匿名变量符“_” a,_,c := 1,2,3 //其中2将不会被赋值 定义类型别名 type 新类型 类型 //ty 阅读全文
posted @ 2021-02-26 09:07 GPHPER 阅读(164) 评论(0) 推荐(0)

TOP