Go language method, interface, reflection, select

Source: Internet
Author: User

Method

Although there is no class, there is still method-go

-Implement a combination with a type by displaying a description of receiver

-method can only be defined for a type in the same package

-receiver can be a value or pointer of type

-No method overloads exist

-You can use a value or a pointer to invoke a method, and the compiler will automatically complete the conversion

-In a sense, the method is the syntactic sugar of the function, because receiver is actually the first parameter received by the method

-If the external structure and the embedded structure have a method with the same name, the method of calling the external structure is preferred

-type aliases do not have the methods that are included with the underlying type

-Method can call a non-public field in a struct

Interface interface

-an interface is a collection of one or more method signatures


Reflective Reflection



Concurrent concurrency

Channel

-channel is the bridge of goroutine communication, mostly blocking synchronous

-Created by make, close off

-channel is a reference type

Package Main

Import "FMT"

Import "Time"

Func Main () {

C: =make (chan bool)

Go func () {

Fmt. Println ("Go Go Go")

C <-true

}()

<-c

Time. Sleep (2*time. Second)


}


-You can use the for range to iterate through the channel

Package Main

Import "FMT"

Import "Time"

Func Main () {

C: =make (chan bool)

Go func () {

Fmt. Println ("Go Go Go")

C <-true

Close (c)

}()

For V: =range c{

Fmt. Println (v)

}


}


-Single or bidirectional channels can be set

-You can set the cache size without blocking until it is filled

Package Main

Import "FMT"

Import "Runtime"

Func Main () {

Runtime. Gomaxprocs (runtime. NUMCPU ())

C: =make (Chan bool,10)

For I: =0;i<10;i++{

Go Go (c,i)

}

For I: =0;i<10;i++{

<-c

}

}

Func Go (c chan bool,index int) {

A: =1

For I: =0;i<100000000;i++{

A+=1

}

Fmt. Println (Index,a)

C<-true


}

Equivalent//

Package Main

Import "FMT"

Import "Runtime"

Import "Sync"

Func Main () {

Runtime. Gomaxprocs (runtime. NUMCPU ())

WG: =sync. waitgroup{}

Wg. ADD (10)

For I: =0;i<10;i++{

Go Go (&wg,i)

}

Wg. Wait ()

}

Func Go (WG *sync. Waitgroup,index int) {

A: =1

For I: =0;i<100000000;i++{

A+=1

}

Fmt. Println (Index,a)

Wg. Done ()


}


Select

-can handle the sending and receiving of one or more channel

-Processing in random order when there are multiple channel available

-Free Select to block the main function

-Can set timeout


This article is from the "Dbaspace" blog, make sure to keep this source http://dbaspace.blog.51cto.com/6873717/1963461

Go language method, interface, reflection, select

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.