Directory
- methods, interfaces, and concurrency
- Method
- Interface
- Concurrent
- Channel
- Conclusion
The fourth article in the Go Language, which focuses on the methods in the go language, includes pointers, structs, arrays, slices, mappings, function closures, etc., each of which provides examples that can be run directly.
methods, interfaces, and concurrency methods
Method is a class of functions with special receiver (struct) parameters
- By structure. Method invocation
Example:
type city struct { name, address string}func (c city) sysCity() { c.name = "帝都" fmt.Println(c.name, c.address)}func testFunc() { c := city{ "北京", "二环", } c.sysCity() fmt.Println(c)}
//就在struct类型前加个*就是获取结构体指针了func (c *city) sysCity() { c.name = "帝都" fmt.Println(c.name, c.address)}
Interface
Use the interface keyword to represent an interface that is a collection defined by a set of method signatures
Example:
//接口type adder interface { syso()}//隐式调用,无需声明func (x city) syso() { fmt.Println(x.name, x.address)}func main() { var var1 adder var2 := city{ "长安", "皇城", } var1 = &var2 var1.syso() //还可以简写成这样 var var3 adder = city{ "帝都", "皇城", } var3.syso()}
Null interface: Interface {} no method
To determine whether an interface value holds a particular type, the type assertion can return two values: its underlying value and a Boolean value that reports whether the assertion was successful.
Example: T, OK: = I. (t)
If I save a T, then T will be its underlying value, and OK is true.
Otherwise, OK will be false and T will be a 0 value of type T, and the program will not panic.
func main() { var i interface{} = "this go" v, ok := i.(string) fmt.Println(v, ok) v, ok := i.(int)}
Concurrent
Command go a new thread called Go, the go is equivalent to another thread
Example:
func hello(x string) { for i := 0; i < 5; i++ { time.Sleep(100 * time.Millisecond) fmt.Println(x) }}func main() { go hello("go") hello("say")}
Channel
A channel is a pipeline with a type that you can use to send or receive values using the channel operator <-.
("Arrow" is the direction of the data flow)
func sum(x int, var1 chan int) { sum := x for i := 0; i < 5; i++ { time.Sleep(100 * time.Millisecond) sum = sum + i } var1 <- sum}func main() { var1 := make(chan int) go sum(10, var1) go sum(20, var1) x, y := <-var1, <-var1 fmt.Println(x + y)}
CH: = Make (chan int, 100)
- 100 Specify buffer size
- My understanding is the amount that can be accommodated in the channel.
Try outputting the following two-segment code, or change 1 to 2, and experience the difference:
func main() { ch := make(chan int, 1) ch <- 21 fmt.Println(<-ch) ch <- 100 fmt.Println(<-ch)}
func main() { ch := make(chan int, 1) ch <- 21 ch <- 100 fmt.Println(<-ch) fmt.Println(<-ch)}
Conclusion
First knowledge of Go language series
- [x] First knowledge of the go language
- [x] First knowledge of Go language: Grammar
- [x] Beginners Go language: Data type
- [x] Initial Go language: method, interface and concurrency
Follow the public number
First knowledge of Go language: methods, interfaces and concurrency