Monthly: 3 月 2020

Golang channel

Jackey Golang 2,455 次浏览 ,
基本介绍 channel本质就是一个数据结构-队列 数据是先进先出(FIFO: first in first out) 线程安全,多goroutine访问时,不需要加锁,也就是说,channel本身就是线程安全的 channel有类型,一个string的channel只能存...

Golang goroutine的调度模型

Jackey Golang 3,874 次浏览 ,
基本介绍 M:操作系统的主线程(是物理线程) P:协程执行需要的上下文 G:协程 MPG模式运行的状态1 当前程序有三个M,如果三个M都在一个CPU上运行,就是并发,如果在不同的CPU上运行,就是并行。 M1,M2,M3...

Golang goroutine

Jackey Golang 2,078 次浏览 ,
进程和线程介绍 进程就是程序在操作系统中的一次执行过程,是系统进行资源分配和调度的基本单位。 线程是进程中的一个执行实例,是程序执行的最小单元,它是比进程更小的能独立运行的基本单位。 一个进程可以创建和销毁多...

Golang 命令行参数

Jackey Golang 4,342 次浏览
test.go package main import ( "fmt" "os" ) func main() { fmt.Println("命令行参数有:", len(os.Args)) for i, v := range os.Args { fmt.Printf("args[%v]=%v\n", i, v) } } linux执行命令编...

Golang 文件操作

Jackey Golang 3,892 次浏览 ,
读取文件(带缓冲区) readFile.go package main import ( "bufio" "fmt" "io" "os" ) func main() { // 打开文件 file, err := os.Open("/Users/jackey/Downloads/3.txt") if err != nil { fmt.P...

Golang 类型断言

Jackey Golang 3,705 次浏览 ,
示例 assert.go package main import "fmt" type Point struct { x int y int } func main() { var a interface{} var point Point = Point{1, 2} a = point var b Point b = a.(Point) //类型断言...

Golang 1.14 新特性整理

Jackey Golang 14,552 次浏览
go mod modfile 引入 练习 目录结构: common |-go.mod mod1 |-demo14mod |-go.mod |-main.go 代码 common/go.mod module demo14mod go 1.13 require github.com/pkg/errors v0.9.1 mod1/main.go package ...

Golang 接口

Jackey Golang 3,720 次浏览 ,
package main import "fmt" // 定义一个接口 type Usb interface { // 声明两个没有实现的方法 Start() Stop() } type Phone struct { } // 让Phone 实现Usb接口的方法 func (p Phone) Start() { fmt....

Golang 继承

Jackey Golang 4,523 次浏览 ,
package main import "fmt" type Student struct { Name string Age int Score float64 } // 将公有的方法绑定到Student 结构体 func (s *Student) GetInfo() { fmt.Printf("学生姓名=%v, 年龄=%v,成绩=%v \...

Golang 封装

Jackey Golang 3,744 次浏览 ,
person.go package model import "fmt" type person struct { Name string age int // 其他包不能访问 salary int } // 写一个工厂模式的函数,相当于构造函数 func NewPerson(name string) *person { r...

Golang 工厂模式

Jackey Golang 3,777 次浏览 ,
student.go package model type student struct { Name string age int } // 结构体student 首字母小写,因此只能在 model 内部使用 // 如果想外部调用,我们可以通过工厂模式解决 func NewStudent(name string, age ...

go get 超时的解决方法

Jackey Golang 5,313 次浏览
GO 1.13 及以上(推荐) Windows $ go env -w GO111MODULE=on $ go env -w GOPROXY=https://goproxy.cn,direct macOS 或 Linux $ export GO111MODULE=on $ export GOPROXY=https://goproxy.cn 然后再执行 go get

Golang 方法中指针传值的陷阱

Jackey Golang 3,379 次浏览 ,
package main import "fmt" type Person struct { Name string } func (p Person) test1() { p.Name = "www.gopher.cc" fmt.Println(p.Name) } func (p *Person) test2() { p.Name = "www.gopher.cc" ...
Go