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

Source: Internet
Author: User

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

Of course not remember:)

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

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6E/0D/wKiom1VyXUySUg-RAAFLOm4XEBo344.jpg "title=" Simple.jpg "alt=" Wkiom1vyxuysug-raaflom4xebo344.jpg "/>

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!

To develop 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 remember name, so that a glance 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.

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

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

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/BF/wKiom1WEubOCL159AADGA5swt3o258.jpg "title=" Structure diagram. png "alt=" wkiom1weubocl159aadga5swt3o258.jpg "/>

The Common.go code above specifies the package (common) it belongs to through the "packages keyword", and the next step is how to use the IsEmpty () function in Common.go.

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

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6E/BC/wKioL1WEvLDA4dYlAACwCf6nSmo771.jpg "title=" Structure Figure 2.png "alt=" Wkiol1wevlda4dylaacwcf6nsmo771.jpg "/>

The above Launcher.go imports the path (util) of the package (common) through the "Import keyword", which is then implemented by the "package name. Function name (Common.isempty ())" method.


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:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6E/E1/wKioL1WKqyGgw0L6AAB7K6GYY48275.jpg "title=" function structure. png "alt=" wkiol1wkqyggw0l6aab7k6gyy48275.jpg "/>

(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) {

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, error) {

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 {


For _, Element: = Range A {

Joinstr + = Element




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 {


For _, Element: = Range A {

Joinstr + = Element




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)


For _, Element: = Range A {

Joinstr + = Element




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

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

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.