"Go Language" "11" Go language packages and functions

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Remember the Getting started example of the "2" Sublime configuration Go development environment?

Of course not remember:) listen

This article is far from the last time, the distant I have almost forgotten. Just stick it.

The func main () in this example indicates that this is a method called Main, and the package main indicates that the method is in the main packet, and the contents of these two parts are the knowledge shared by Ben SectionTo.

So simple?

Yes, simple but not minimalist, hehe, then look down!


Listen and listen to the development of an application system, taking into account the readability of the code, tend to split the code into different packages, and then give the package a good name, so that you can understand the purpose of the package, such as the math package, this is the go language itself, the math-related package, the package below the absolute value of the source file Abs.go And the asin.go of the sine.

Listen, listen, listen.

1, then what is a package? Let's look at an example:

Listen and listen. Create a Common.go source file under E:\GO\workspace\pwm\src\util, fill in the following content

Listen, listen, listen.

Listen and hear the Common.go code above specifies the package (common) that it belongs to through the "packages keyword", and then how to use the IsEmpty () function in Common.go?

Listen and listen. Create a Launcher.go source file under E:\GO\workspace\pwm\src, fill in the following content

Listen, listen, listen.

Listen and listen to the above Launcher.go import the path (util) of the package (common) through the "Import keyword", and then implement the call by "package name. Function name (Common.isempty ())".

Note:

There may be people wondering, how could this be? The usual invocation is written in a manner similar to the following:

Import FMT

FMT. Println ("Import fmt,then use FMT's function to invoke")

Explanation: The reason for this is that "package name" and "path" use the same name. In order to express the package clearly, the "package" and "package path" are deliberately used with different names.


2. Functions

The function is composed of the keyword Func, the function name, the argument list, the return value, and the function body, as follows:


(1) If the parameter list has the same type parameter, as the previous example can be abbreviated as:

Func Add (A, B int) (ret int, err error) {

Listen to return a + B, nil

}

(2) Of course, when the function is called, the caller does not care about the return value and does not even use the return variable name, so the above example can be abbreviated as:

Func Add (A, b int) (int, hear error) {

Listen to return a + B, nil

}

(3) Readers of the first use of go may be interested in the "multiple return value", the previous use of other languages are laborious to return multiple values, did not think go to help the program Ape realized:)


3, the function of the indefinite parameters

Indefinite parameters are not new things, Java6 already have this thing. The so-called indefinite parameter is not clear the number of parameters exactly how much, with examples to illustrate:

Func Join (A, B string) string {

Return a + b

}

The function implements the connection of the two arguments strings, so that the caller only needs a join ("a", "B"), since there are only two parameters.


Then think down, if you need to concatenate multiple strings together? You might expect to change the entry parameter into an array of strings, as follows:

Func Join (a []string) string {

JOINSTR: = ""


For _, Element: = Range A {

Joinstr + = Element

}


Return JOINSTR

}

However, this leads to a call problem where the caller must first initialize an array or slice and then make the call, as follows:

s: = []string{"A", "B", "C", "D", "E"}

Fmt. Println (common. Join(s))


If you want the caller to not construct an array or slice, treat the entry as if it were a normal string, and this involves the concept of "indeterminate parameters", as follows:

Func Join (a ... string) string {

JOINSTR: = ""


For _, Element: = Range A {

Joinstr + = Element

}


Return JOINSTR

}

The only way to write with the above is to use "... string", as the caller treats the generic string as follows:

Fmt. Println (Common. Join ("A", "B", "C", "D", "E"))


This indefinite parameter looks more elegant, and there is a problem: If the entry is not all the same type? For example, there is a string in the argument is also shaped, you must put the indefinite parameters at the end, otherwise it will be reported similar to can only use ... as final argument in list error

Func Join (i int, a ... string) string {

Fmt. Println ("----------------", i)

JOINSTR: = ""


For _, Element: = Range A {

Joinstr + = Element

}


Return JOINSTR

}


4. Concluding remarks

I've seen Xu Xiwei, cloud power about go of the book, there is no smell of the video, all talk about the closure of the problem, in fact, the closure concept a long time ago JS is in use, here do not want to talk about the problem of closures, because in the process of writing the program I think everything is natural, write a certain code will naturally consider the elegance of the Code, The closure is used when the room is not noticed.


This article is from the "Green Guest" blog, please be sure to keep this source http://qingkechina.blog.51cto.com/5552198/1665234

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.