The first article from the public number: Go programming time

“Go programming time”, a can take you to learn the Go language column, at the same time welcome to search my namesake public account [Go programming time] (beautiful layout is more suitable for reading), the first time to get Go language dry materials.

A series of reading

1. Set up the Go language development environment


For Python only experienced friends, may not understand the word declaration, in Python directly used, also do not need to declare the type of what.

Go is a statically typed language. Since the compiler checks the type of a variable at compile time, it requires that all variables have an explicit type.

Variables must be declared before they can be used. By declaring a type, you stipulate that you can assign only values of that type to a variable.

The first two of these declarations can also be used to define constants by making the keyword var const.

First: declare a variable in one line

var <name> <type>
Copy the code

Var is the keyword (fixed), name is the variable name, and type is the type.

With var, even though the type is specified, Go implicitly initializes it, such as string to an empty string, int to 0, float to 0.0, bool to false, and pointer to nil.

If you want to initialize it during the declaration process, you can write it like this

var name sting = "Python Programming Time"
Copy the code

The complete code in the Go file is as follows. In order not to write repetitive code, the complete code will not be posted later, only the key code will be posted

package main

import "fmt"

func main(a)  {
	var name string = "Python Programming Time"
	fmt.Println(name)
}
Copy the code

The rvalue (the value to the right of the equals sign, rvalue) is clearly a string. (Note that double quotes are equivalent to single quotes in Python, but double quotes are not the same as single quotes in Go. It is important to use double quotes for strings and single quotes for rune characters. This will be covered separately later), so it can also be simplified to

var name = "Python Programming Time"
Copy the code

If your rvalue has a decimal point, the compiler will declare your variable float64 if you do not specify the type, but in many cases this precision is not required.

In this case, it is recommended to specify the type and not be lazy

var rate float32 0.89
Copy the code

Second: multiple variables are declared together

Declaration of multiple variables, in addition to the above can be written as multiple lines, can also be written as follows

var (
	name string
	age int
	gender string
)
Copy the code

Third: declare and initialize a variable

Use := (Deductive declarative or short type declarative: the compiler automatically deduces the type of an lvalue from an rvalue type.) You can declare a variable and initialize it (explicitly).

name := "Python Programming Time"

/ / equivalent to the

var name string = "Python Programming Time"

/ / equivalent to the

var name = "Python Programming Time"
Copy the code

There is a limitation to this method, however, that it can only be used inside functions

Fourth: Declare and initialize multiple variables

name, age := "wangbm".28
Copy the code

This method is also often used for variable exchanges

var a int = 100
var b int = 200
b, a = a, b
Copy the code

The new function declares a pointer variable

I’m going to talk a little bit about Pointers.

Variables are divided into two types: ordinary variables and pointer variables

Ordinary variables store the data itself, while pointer variables store the address of the data.

As shown in the following code, age is a normal variable that stores 28, and PTR is the memory address that stores the value of age: 0xC000010098

package main

import "fmt"

func main(a)  {
	var age int = 28
	var ptr = &age  // & is followed by the name of the variable, indicating the memory address of the variable
	fmt.Println("age: ", age)
	fmt.Println("ptr: ", ptr)
}
Copy the code

The output

age:  28
ptr:  0xc000010098
Copy the code

And the new function that we’re talking about here, is a built-in function in Go.

Use the expression new(Type) to create an anonymous variable of Type Type, initialize it to a zero value of Type Type, and return the address of the variable with a pointer of Type *Type.

package main

import "fmt"

func main(a)  {
	ptr := new(int)
	fmt.Println("ptr address: ", ptr)
	fmt.Println("ptr value: ", *ptr)  // * Fetch the value from the memory address
}

Copy the code

The output

ptr address:  0xc000010098
ptr value:  0
Copy the code

Creating variables with new is no different from creating variables with normal variable declaration statements. In addition to not having to declare the name of a temporary variable, we can also use new(Type) in expressions. In other words, the new function is more of a syntactic sugar than a new foundational concept.

The two ways of writing it are equivalent

/ / use the new
func newInt(a) *int {
    return new(int)}// Use traditional methods
func newInt(a) *int {
    var dummy int
    return &dummy
}
Copy the code

Either way, a variable/constant can only be declared once, and multiple declarations will result in an error.

There are exceptions, however, and that brings us to one special variable: anonymous variables, also known as placeholders, or blank identifiers, represented by underscores.

Anonymous variables have three advantages:

  • No memory is allocated and no memory space is occupied
  • You don’t have to worry about naming useless variable names
  • Multiple declarations won’t cause any problems

Usually we use anonymous accept values that must be received but will not be used.

func GetData(a) (int.int) {
    return 100.200
}
func main(a){
    a, _ := GetData()
    _, b := GetData()
    fmt.Println(a, b)
}
Copy the code