随笔分类 -  037-golang

摘要:golang的各种预置的变量类型int string等等都是实现了interface{} package main import "fmt" //interface{}是万能数据类型 func myFunc(arg interface{}) { fmt.Println("myFunc is call 阅读全文
posted @ 2021-03-18 01:53 从程序员到CTO 阅读(206) 评论(0) 推荐(0)
摘要:1 基类接口 // 基类,接口 type AnimalIF interface { Sleep() GetColor() string //获取动物的颜色 GetType() string //获取动物的种类 } 2 实现类-猫 //具体的类 type Cat struct { color stri 阅读全文
posted @ 2021-03-18 01:42 从程序员到CTO 阅读(106) 评论(0) 推荐(0)
摘要:golang的继承的实现机制,并没有使用extend关键字,而是使用了设计模式中推荐的组合模式。 从一系列的机制来看,golang的设计原则貌似是保持尽量少的关键字,于是我们程序员就不得不用struct+外部函数的方式实现类,不得不用组合模式显式的实现继承机制。 基类: type Human str 阅读全文
posted @ 2021-03-18 01:01 从程序员到CTO 阅读(200) 评论(0) 推荐(0)
摘要:golang的设计原则是尽量减少关键字,因此, 没有class,用结构体实现类,用标准c的方法或者c++编译器的方法,使用this参数和外部函数实现类方法。 没有extend,用组合式设计模式实现继承 没有private,public,用大小写表示是否允许外部访问。 从语言简洁度的角度来说,这个思路 阅读全文
posted @ 2021-03-18 00:50 从程序员到CTO 阅读(58) 评论(0) 推荐(0)
摘要:现在版本的golang中没有class关键字。golang沿用了标准c对类的模拟,或者c++编译器中对OOP的实现:基于结构体struct实现了类的机制。 package main import "fmt" //如果类名首字母大写,表示其他包也能够访问 type Hero struct { //如果 阅读全文
posted @ 2021-03-18 00:34 从程序员到CTO 阅读(286) 评论(0) 推荐(0)
摘要:与c/c++一样 //type 关键字可以定义类型的别名 type myint int //type 关键字可以定义一个结构体 type Book struct { title string auth string } // 传值 func changeBook(book Book) { //传递一 阅读全文
posted @ 2021-03-18 00:11 从程序员到CTO 阅读(61) 评论(0) 推荐(0)
摘要:1 package main 2 3 import "fmt" 4 5 func printMap(cityMap map[string]string) { 6 //cityMap 是一个引用传递 7 for key, value := range cityMap { 8 fmt.Println(" 阅读全文
posted @ 2021-03-17 19:55 从程序员到CTO 阅读(125) 评论(0) 推荐(0)
摘要:// > 第一种声明方式 //key是string, value是string var myMap1 map[string]string //在使用map前, 需要先用make给map分配数据空间 myMap1 = make(map[string]string, 10) myMap1["one"] 阅读全文
posted @ 2021-03-17 19:45 从程序员到CTO 阅读(122) 评论(0) 推荐(0)
摘要:(1) 追加 // 容量为5,但合法元素数为3 var numbers = make([]int, 3, 5) fmt.Printf("len = %d, cap = %d, slice = %v\n", len(numbers), cap(numbers), numbers) //向numbers 阅读全文
posted @ 2021-03-17 19:44 从程序员到CTO 阅读(415) 评论(0) 推荐(0)
摘要:// 只声明了指针,并没有分配内存 var slice1 []int // 申请三个元素的内存 slice1 = make([]int, 3) if slice1 == nil { fmt.Println("slice1 是一个空切片") } else { fmt.Println("slice1 是 阅读全文
posted @ 2021-03-17 19:41 从程序员到CTO 阅读(136) 评论(0) 推荐(0)
摘要:(1) 固定大小数组 1 package main 2 import "fmt" 3 4 //值拷贝 5 func printArray(myArray [4] int) { 6 7 8 for index, value := range myArray { 9 fmt.Println("index 阅读全文
posted @ 2021-03-17 19:39 从程序员到CTO 阅读(584) 评论(0) 推荐(0)
摘要:package main import "fmt" func main() { //写入defer关键字 defer fmt.Println("main end1") defer fmt.Println("main end2") fmt.Println("main::hello go 1") fmt 阅读全文
posted @ 2021-03-17 19:37 从程序员到CTO 阅读(61) 评论(0) 推荐(0)
摘要:跟C/C++一样 阅读全文
posted @ 2021-03-17 19:36 从程序员到CTO 阅读(45) 评论(0) 推荐(0)
摘要:在lib中,如果一个函数是大写的,则是可以外部调用的。如果函数是小写的,则只能内部调用。 阅读全文
posted @ 2021-03-17 19:33 从程序员到CTO 阅读(248) 评论(1) 推荐(0)
摘要:(1) 单返回值 1 func foo1(a string, b int) int { 2 3 fmt.Println("a = ", a) 4 fmt.Println("b = ", b) 6 9 c := 100 10 return c 12 13 } (2) 匿名多返回值 func foo2( 阅读全文
posted @ 2021-03-17 13:37 从程序员到CTO 阅读(228) 评论(0) 推荐(0)
摘要:0 const length int = 10 1 //const 来定义枚举类型 2 const ( 3 //可以在const() 添加一个关键字 iota, 每行的iota都会累加1, 第一行的iota的默认值是0 4 BEIJING = iota //iota = 0 5 SHANGHAI / 阅读全文
posted @ 2021-03-17 13:34 从程序员到CTO 阅读(98) 评论(0) 推荐(0)
摘要:1 var b int = 100 2 3 // 可以不指定类型,编译器根据值推断类型 4 var c = 100 5 6 fmt.Println("c = ", c) 7 fmt.Printf("type of c = %T\n", c) 8 fmt.Println("c = ", c, ",b 阅读全文
posted @ 2021-03-17 13:33 从程序员到CTO 阅读(66) 评论(0) 推荐(0)
摘要:编译和构建 go build test.go 运行 go run test.go 1 package main 2 import "fmt" 3 func main() 4 { 5 fmt.Println(“HelloWorld”); 6 } 阅读全文
posted @ 2021-03-17 13:32 从程序员到CTO 阅读(70) 评论(0) 推荐(0)
摘要:一 索引 二 特性 三 安装 在vscode中新建go文件,vscode会自动安装提示进行go插件的安装 1 vscode以管理员权限启动 2 解决插件安装失败的问题 VScode中安装Go插件,由于墙的原因很可能会出现如下提示 Installing github.com/mdempsky/goco 阅读全文
posted @ 2021-03-17 00:45 从程序员到CTO 阅读(88) 评论(0) 推荐(0)

交流加我QQ:39667545