First knowledge of Go language: methods, interfaces and concurrency

Source: Internet
Author: User

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)}
    • Fmt. PRINTLN (c) Output or {Beijing second ring}, does not change the value of a struct because it assigns a value to C.name in Syscity ()

      The pointer receiver says that you can change the value of the struct by the pointer, so you can also change the value of the struct by the pointer receiver.
//就在struct类型前加个*就是获取结构体指针了func (c *city) sysCity() {    c.name = "帝都"    fmt.Println(c.name, c.address)}
    • Try to output a bit
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")}
    • Output say go
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.