[Go language] Learn go--function and method from Docker source

Source: Internet
Author: User

function and Method relationships

method is a function defined for a type,

function can be called separately, and method must be called on an instance of a type

// function Invocation Mode Packagename.funcname () // Method Call Mode var T PackageName.Typet.MethodName ()
Source
Func (CLI *dockercli) Loadconfigfile () (err error) {cli.configfile, err= registry. Loadconfig (OS. Getenv ("HOME"))    ifErr! =Nil {fmt. fprintf (Cli.err,"WARNING:%s\n", Err)} returnErr}func newdockercli (inchIo. Readcloser, out, err Io. Writer, Proto, addrstring, Tlsconfig *tls. Config) *dockercli {...return&dockercli{Proto:proto, addr:addr,inch:inch,         out: out,                ...    }}

Loadconfigfile () is a method, while NEWDOCKERCLI () is a function.

Function
func functionname (parameter_list) (return_value_list) {...} // parameter_list form (param1 type1, param2 type2, ...) // return_value_list form (Ret1 type1, Ret2 type2, ...)

Note: function can return multiple parameters, and if you do not want to accept a parameter at the time of the call, use the underscore _

_, exist = functionname (...)
Problem with value/reference passing

By default, parameters in a function are passed by value, and if you want to modify the value of the original data inside a function, you need to pass a reference with the & symbol.

Exceptions: Reference types are passed by reference by default, such as slices, maps, interfaces, channels.

int {    =int) {    value2:= &value    = NewValue}

You can define the last parameter by the type of ... Type to pass multiple parameters of the same type

Func Min (INTs ...int)int {
The ints type is []int{}, and the value is the value passed in. ifLen (ints) = =0 { return-1} min:= ints[0] for_, X: =Range INTs {ifX <min {min=x}}returnmin}//calledFmt. Println (Min (4,6,9,Ten,3,2) arr:= []int{9,5,3,Ten, -,2}fmt. Println-Min (arr ...)

What is described above is the case of passing in multiple of the same type, if multiple of different types are defined?

There are two methods, the first of which is to define a new struct, contain these different types, and then take the struct type as the input parameter, and the second is to use the empty interface.

This article mainly introduces the second kind

Func Printtype (Variables ...Interface{}) {     for_, V: =Range Variables {Switchv. (type) { Case int: FMT. Println ("type is int%d", V)default: FMT. Println ("Other type%v", V)} }}func showfunctionmultiinterfaceparameters () {lemon. Printtype (5,"AAAA") Var2:= []Interface{}{6,7,9,"BBB","CCC"} lemon. Printtype (Var2 ...)}

Note: You need to determine the type within the method and then proceed.

Defer

Defer is used to define the statement that is executed at the end of the method before the return.

Note: In the same method, two defer are defined successively, the order of execution is reversed and LIFO.

func Deferorder () { forI: =0; I <4; i++{defer FMT. Println ("index value is%d", I)}}//OutputIndex value is%d3Index Value is%d2Index Value is%d1Index Value is%d0
Method

method is a special function, defined on a particular type, is called by an instance of the type, and this instance is called receiver.

The receiver type can be any type, including the function type, but receiver cannot be a interface type.

Func (recv receiver_type) methodName (parameter_list) (return_value_list) {...}

Note: Method and its type must be defined within a package! But we can do it in other ways.

Solution: How can you make the original type defined in our own package?

Two methods:

The first, through alias.

Int

This defines the method for the Int.

The second, through the anonymous attribute

struct {    int}

Two methods, the second is better, because the first one only for the current alias is useful, reusability is not as good as the second method.

If you want to modify the receiver's property value inside method, Recv *receiver_type (adds a * to the type) to indicate an incoming reference.

It's okay to pass in the value at this time, the go language automatically transforms, T.func to (&t). Func.

Func (b *b) Change () {...} var B1 bb1.change ()
Summarize

The go language defines the method and type separately.

The advantage is that we can add methods to any type without having to modify the original code.

[Go language] Learn go--function and method from Docker source

Related Article

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.