go and declare variables

go and declare variables

It would be wonderful start and end, by writing x = 4, we look at the variables, we can say and declare a variable assignment, but unfortunately, go language variable declaration and assignment is more complicated than that. By learning some simple examples to start learning variable declaration and assignment. Then in the next chapter, when we create and use structures, we will be in-depth study. Nevertheless, you need to spend some time to adapt.

You might be surprised, why so complicated, let's start with some examples of learning.

In go the most direct way to declare variables and assign the most complicated:

package main

import (
    "fmt"
)
func main() {
    var power int
    power = 9000
    fmt.Printf("It's over %d\n", power)
}

Here we declare a intvariable power. By default, go will powerassign a 0value. Fu integer 0, boolean Fu false, Fu string ""and so on. We then 9000assigned to the variable power. We can also be merged into one line 2 lines of code:

var power int = 9000

Although the need to enter a lot. There is a simple and convenient go variable declaration operators: :=, go type of the variable can be inferred:

power := 9000

Here's a simple wording, can work by function:

func main() {
    power := getPower()
}
func getPower() int {
    return 9001
}

It should be remember that :=for declaring a variable and assign values to variables. Why is this so? Since a variable can not be declared 2 views (not in the same code range). If you try to run the following code, you'll get an error message.

func main() {
    power := 9000
    fmt.Printf("It's over %d\n", power)
    // COMPILER ERROR:
    // no new variables on left side of :=
    power := 9001
    fmt.Printf("It's also over %d\n", power)
}

The compiler will prompt :=the left is not a new variable. This means that when we first declare a variable, we use :=. But in the subsequent assignment, we want to use =. There are a lot of sense, but also at any time to remind you when to use the :=and =.

If you carefully read the error message, you will find that there are multiple variables. Because go to support several variables at once assigned (use =or :=):

func main() {
    name, power := "Goku", 9000
    fmt.Printf("%s's power is over %d\n", name, power)
}

In addition, if a variable is a new variable you can also use the :=assignment. E.g:

func main() {
    power := 1000
    fmt.Printf("default power is %d\n", power)

    name, power := "Goku", 9000
    fmt.Printf("%s's power is over %d\n", name, power)
}

Although the variables powerused :=, but the compiler does not use the 2nd :=being given, because here there is a variable name, which is a new variable, allowing the use :=. But you can not change the powertype. It has been declared as an integer, it can only be assigned integer.

Now, the last thing you need to know is that a similar package to import, go program can not exist unused variables, such as:

func main() {
    name, power := "Goku", 1000
    fmt.Printf("default power is %d\n", power)
}

This code will not compile, because the variable namehas been declared, but not used. Similar import package is not used, it might make you a little disappointed, but the whole, I think this is to make the code more concise and readable.

The next presentation is not assignment statement and relevant content about the variables. Now, you just need to remember to use var NAME TYPEto declare a variable, and the value of its initial value of zero corresponding type of variable, use NAME := VALUEdeclare a variable and assign the use NAME = VALUEto have been declared to the variable assignment.
This switched: http://codingdict.com/article/22520

Guess you like

Origin www.cnblogs.com/bczd/p/11983085.html