Go Language notes

Source: Internet
Author: User

1. = is not applicable to functions. To declare a global variable, use the keyword var.

2. Constant is

const Name = value

3. If the variable needs to declare the type:

xxx int:=1;

4. the Boolean value is of the bool type, and the value is true or false. The default value is false.

5. The variable type conversion in the go language must be a display conversion, that is, related code.

6. Text and other types cannot be directly connected through +. type conversion is required first.

7. variables in the go language can be applied in groups.

var(    i int    pi float32    prefix string)

8. Variables starting with an uppercase letter can be exported, that is, variables that can be read from other packages are public variables. Variables starting with a lowercase letter cannot be exported and are private variables.

The same is true for functions starting with an uppercase letter, which is equivalent to a public function with a public keyword in the class. A private function with a private keyword starts with a lowercase letter.

9. The array is declared as follows:

var arr [n]type

10. Use slice to declare Dynamic Arrays

11. If conditions are not enclosed in parentheses

12. Add a colon to the end of the name to indicate the label, and then use goto to jump:

here:goto here

13. For statement:

 for index:=0; index < 10 ; index++ {        sum += index    }

14. syntax similar to while:

sum := 1for sum < 1000 {    sum += sum}

15. The go language can return multiple values:

func SumAndProduct(A, B int) (int, int) {    return A+B, A*B}

16. pointer usage

// A simple function that implements the func Add1 (A * INT) int operation with the parameter + 1 {// note that, * A = * A + 1 // modify the value of a return * A // return the new value} func main () {X: = 3 FMT. println ("x =", x) // output "x = 3" X1: = Add1 (& X) // call Add1 (& X) to transmit the address FMT of X. println ("x + 1 =", X1) // output "x + 1 = 4" FMT. println ("x =", x) // output "x = 4 "}

17. Using the keyword defer, the program can be skipped and then returned for execution after execution.

func ReadWrite() bool {    file.Open("file")    defer file.Close()    if failureX {        return false    }    if failureY {        return false    }    return true}

18. If there are many calls to defer, defer adopts the post-in-first-out mode, so the following code will output 4 3 2 1 0

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.