摘要:```go package main import ( "fmt" "time" ) func main() { c1 := make(chan string) c2 := make(chan string) go func() { time.Sleep(time.Second 1) c1
阅读全文
摘要:```go package main import "fmt" func main() { pings := make(chan string, 1) pongs := make(chan string, 1) ping(pings, "passwd message") pong(pings, po
阅读全文
摘要:```go package main import ( "fmt" "time" ) func main() { //缓存通道 done := make(chan bool,1) go worker(done) //等待读取chan,当chan没数据时会阻塞
阅读全文
摘要:```go package main import "fmt" func main() { //缓存通道 msg := make(chan string,2) msg
阅读全文
摘要:```go package main import "fmt" func main() { //string类型通道 messages := make(chan string) //往通道写入数据 go func() {messages
阅读全文
摘要:```go package main import "fmt" func main() { //main gorouting中调用f函数 f("direct") //重新建一个goroutine执行f函数 go f("goroutine") //重新建一个goroutine执行函数 go func(
阅读全文
摘要:```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)
}
...
阅读全文
摘要:```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...
阅读全文
摘要:```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...
阅读全文
摘要:```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...
阅读全文
摘要:```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...
阅读全文
摘要:```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)
}
```
阅读全文
摘要:```go
package main import "fmt" func main() { //这里需要将闭包函数当作一个类理解,这里是实例化 nextInt := intSeq() fmt.Println(nextInt()) fmt.Println(nextInt()) fmt.Println(nextInt()) //重新实例化 newInts := intSeq() ...
阅读全文
摘要:```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...
阅读全文
摘要:```go
package main import "fmt" func main() { a,b := vals() fmt.Println(a) fmt.Println(b)
} //函数多个返回值
func vals() (int,int) { return 3,7
}
```
阅读全文
摘要:```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
}
```
阅读全文
摘要:```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 ==...
阅读全文
摘要:```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...
阅读全文
摘要:```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
阅读全文
摘要:```go package main import "fmt" func main() { //定义一个数组并完成初始化,初始值为对应的零值 var a [5]int fmt.Println("emp:",a) a[4] = 100 fmt.Println("set:",a) fmt.Println
阅读全文