商君

导航

随笔分类 -  Go基础实例学习

上一页 1 2 3 下一页

通过代码学习Golang基础知识
Go Example--通道选择器
摘要:```go package main import ( "fmt" "time" ) func main() { c1 := make(chan string) c2 := make(chan string) go func() { time.Sleep(time.Second 1) c1 阅读全文

posted @ 2018-10-19 15:13 漫步者01 阅读(103) 评论(0) 推荐(0)

Go Example--通道方向
摘要:```go package main import "fmt" func main() { pings := make(chan string, 1) pongs := make(chan string, 1) ping(pings, "passwd message") pong(pings, po 阅读全文

posted @ 2018-10-19 15:07 漫步者01 阅读(138) 评论(0) 推荐(0)

Go Example--通道同步
摘要:```go package main import ( "fmt" "time" ) func main() { //缓存通道 done := make(chan bool,1) go worker(done) //等待读取chan,当chan没数据时会阻塞 阅读全文

posted @ 2018-10-17 19:58 漫步者01 阅读(89) 评论(0) 推荐(0)

Go Example--缓存通道
摘要:```go package main import "fmt" func main() { //缓存通道 msg := make(chan string,2) msg 阅读全文

posted @ 2018-10-17 19:54 漫步者01 阅读(109) 评论(0) 推荐(0)

Go Example--通道
摘要:```go package main import "fmt" func main() { //string类型通道 messages := make(chan string) //往通道写入数据 go func() {messages 阅读全文

posted @ 2018-10-17 10:20 漫步者01 阅读(67) 评论(0) 推荐(0)

Go Example--协程
摘要:```go package main import "fmt" func main() { //main gorouting中调用f函数 f("direct") //重新建一个goroutine执行f函数 go f("goroutine") //重新建一个goroutine执行函数 go func( 阅读全文

posted @ 2018-10-17 10:18 漫步者01 阅读(97) 评论(0) 推荐(0)

Go Example--错误处理
摘要:```go package main import ( "errors" "fmt" ) //定义一种错误类型 type argError struct { arg int prob string } //实现错误方法 func (e *argError) Error()string { return fmt.Sprintf("%d - %s",e.arg, e.prob) } ... 阅读全文

posted @ 2018-10-16 19:32 漫步者01 阅读(121) 评论(0) 推荐(0)

Go Example--接口
摘要:```go package main import ( "math" "fmt" ) type geometry interface { area() float64 perim() float64 } //rect实现接口 type rect struct { width,height float64 } //cicle实现接口 type cicle struct { radi... 阅读全文

posted @ 2018-10-16 19:11 漫步者01 阅读(86) 评论(0) 推荐(0)

Go Example--方法
摘要:```go package main import "fmt" //定义结构体 type rect struct { width,height int } //定义结构体指针的方法 func (r *rect) area() int { return r.width*r.height } //定义结构体的方法 func (r rect)perim() int { return 2*r... 阅读全文

posted @ 2018-10-16 15:53 漫步者01 阅读(158) 评论(0) 推荐(0)

Go Example--结构体
摘要:```go package main import "fmt" //定义一个私有结构体 type person struct { name string age int } func main() { //结构体的初始化 fmt.Println(person{"Bob",20}) fmt.Println(person{name:"Alice",age:20}) fmt.Prin... 阅读全文

posted @ 2018-10-16 15:48 漫步者01 阅读(86) 评论(0) 推荐(0)

Go Example--指针
摘要:```go package main import ( "fmt" ) func zeroval(ival int) { ival = 0 } func zeroptr(iptr *int) { *iptr = 0 } func main() { i:=1 fmt.Println("initial:",i) //函数是值传递,i发生了复制,所以不会修改原始i的值 zer... 阅读全文

posted @ 2018-10-16 15:43 漫步者01 阅读(102) 评论(0) 推荐(0)

Go Example--递归
摘要:```go package main import "fmt" func main() { fmt.Println(fact(7)) } //函数的递归 func fact(n int) int { if n == 0{ return 1 } return n*fact(n-1) } ``` 阅读全文

posted @ 2018-10-15 19:13 漫步者01 阅读(105) 评论(0) 推荐(0)

Go Example--闭包
摘要:```go package main import "fmt" func main() { //这里需要将闭包函数当作一个类理解,这里是实例化 nextInt := intSeq() fmt.Println(nextInt()) fmt.Println(nextInt()) fmt.Println(nextInt()) //重新实例化 newInts := intSeq() ... 阅读全文

posted @ 2018-10-15 19:04 漫步者01 阅读(77) 评论(0) 推荐(0)

Go Example--变参函数
摘要:```go package main import "fmt" func main() { sum(1,2) sum(1,2,3) nums := []int{1,2,3,4} //nums...将nums切片打平为多个参数 sum(nums...) } //定义变参函数 func sum(nums ...int) { fmt.Println(nums," ") tota... 阅读全文

posted @ 2018-10-15 18:47 漫步者01 阅读(96) 评论(0) 推荐(0)

Go Example--函数多返回值
摘要:```go package main import "fmt" func main() { a,b := vals() fmt.Println(a) fmt.Println(b) } //函数多个返回值 func vals() (int,int) { return 3,7 } ``` 阅读全文

posted @ 2018-10-15 18:44 漫步者01 阅读(106) 评论(0) 推荐(0)

Go Example--函数
摘要:```go package main import ( "fmt" ) func main() { res := plus(1,2) fmt.Println("1+2=",res) } //定义私有函数,首字母小写。首字母大写,定义公有函数 func plus(a,b int)int { return a+b } ``` 阅读全文

posted @ 2018-10-15 18:42 漫步者01 阅读(95) 评论(0) 推荐(0)

Go Example--range
摘要:```go package main import "fmt" func main() { nums := []int{2,3,4} sum :=0 //rang 遍历切片 for _,num := range nums { sum += num } fmt.Println("sum:",sum) for i,num := range nums { if num ==... 阅读全文

posted @ 2018-10-15 14:56 漫步者01 阅读(90) 评论(0) 推荐(0)

Go Example--map
摘要:```go package main import "fmt" func main() { //初始化map make(map[类型][类型]) m:= make(map[string]int) m["k1"]=7 m["k2"]=13 fmt.Println("map:",m) //直接取map中的值,如果key不存在v为对应的零值 v1 := m["k1"] fmt.P... 阅读全文

posted @ 2018-10-15 14:50 漫步者01 阅读(101) 评论(0) 推荐(0)

Go Example--切片
摘要:```go package main import ( "fmt" ) func main() { //make来初始化一个切片,必须指名切片的长度 s:= make([]string, 3) fmt.Println("emp:",s) s[0] = "a" s[1] = "b" s[2] = "c 阅读全文

posted @ 2018-10-15 14:33 漫步者01 阅读(84) 评论(0) 推荐(0)

Go Example--数组
摘要:```go package main import "fmt" func main() { //定义一个数组并完成初始化,初始值为对应的零值 var a [5]int fmt.Println("emp:",a) a[4] = 100 fmt.Println("set:",a) fmt.Println 阅读全文

posted @ 2018-10-15 14:17 漫步者01 阅读(75) 评论(0) 推荐(0)

上一页 1 2 3 下一页