Golang Study notes: Golang Grammar (ii)

Source: Internet
Author: User
Tags case statement loop case uppercase letter


In the grammar (i), we learn the basic elements of the basic data types, variables, constants, and so on in go, and in this section we will learn how to organize these elements and eventually write code that can be executed.

In this section include:

The Process Control statement in go;

The use of functions in go;

Go special error handling method;

Process Control statements in the Golang

In the specific programming, it is necessary to use some special statements to implement certain functions, such as using loop statements to iterate, using the Choice Statement Control program execution mode. These statements are in any one of the programming languages

will be supported in Golang, in addition to support common loops, conditional selection statements, but also support jump statements, the following are the ways to see how these statements are used.

Conditional Statement If, else if, else,...

Usage:

If expression {      //note curly brackets must be on the same line as if, including the following else and {also; the expression does not need parentheses around the body                  //And no matter how many lines in the program require curly braces, this is a difference from C ...} else{Program Body ...}
Note that you can have an assignment for an expression after if, separated by semicolons.SELECT statement Switch

In fact, it is similar to the function of the IF statement, except that the choice statement is more flexible and can reduce the branch overhead of if else, similar to the use of switch in C language,

The switch  expression {//and the IF statement, the expression does not need to use parentheses, while switch and {on the same line case condition one: Program body case Condition Two: Program body case ... default: Program Body}

A big difference between this and the C language is that you don't need to use break to force the end in the body of each case condition. If you need to execute the following case statement, you can do so by adding the Fallthrough keyword.

Loop Statement for

Go also supports looping statements, but unlike other languages such as C, go supports only for loops and does not support using while.

Use mode one: the most basic usage for variable initialization; Cycle end condition; Variable change expression {program body ...} Use mode two: Infinite loop case, similar to C in the for (;;) For {program body ...}

Also allows the use of balanced multiple assignments in the for expression, such as the following to reverse an array in go:

A: = [] int {1,2,3,4,5,6}fori, J: = 0, Len (a)-1; i<j; I, j = i + 1, j-1 {a[i], a[j] = A[j], A[i]}

Note You can also use the break statement in the go language.

Jump Statement Goto

As a beginner, I do not know why go language will support goto this usage, in the early days about the shortcomings of Goto, has been dijkstra and other people sent the article focused discussion, here do not do specific introduction, and so on after-depth study to add, why designers to design.

The above is Golang supported four kinds of program control statements, in order to complete the code, we also need to encapsulate some specific functions, the following we introduce the use of functions in Golang.

the use of functions in Golangdefinition of a function

Unlike other languages, such as the C language, the functions in Golang are defined as follows: Func keyword function name (parameter 1, parameter 2, ...) (return value 1, return value 2, ...)  {function Body ...} For example, the following implementation adds two variables to the function: Func  Add (a int, b int) (ret int, err error) {return a+b, nil}

If the return value is only one then the parentheses can be saved, and if more than one argument is of the same type, then the type of the preceding variable is omitted.

Call to function

The function is called by importing the package and then using the function in the same way as the package.

It is important to note that the case of the function name is Golang in the box, and only the function with the beginning of the uppercase letter can be used by the other package. and use smaller letters.

Functions that begin with can only be visible within this package. The naming conventions for variable names are similar.

support for indeterminate parameters

In Golang, the function also supports indefinite arguments, that is, the function can accept any number of arguments of any type.

For example, the following function MyFunc can accept variables of any number of type int as arguments:

Func myfunc (args ... int) {for_,arg: = range args {fmt. Println (ARG)}}

At call time:

MyFunc (2,3,4)

Mufunc (1,3,7,13)

If you support any type of argument, you only need to change int to interface{}  to be able to func  myprintf (args ... interface{}) {Program body ...}

Multiple return values

I have already introduced the Golang function is to support multiple return values, if the return value is not required then directly can be set to the default of the corresponding value can be.


Anonymous functions and closures

Golang Study notes: Golang Grammar (ii)

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.