Finished Hello World, with some caveats Go

Preface

Go but that is more fire in recent years, the language of the current version has reached 1.13 , if there is basis for other languages, learning Go was quite simple. This article is a summary of some other languages might not the same place.

Multi return value

If a method like before return multiple values, but also a corresponding way, such as Python , and Javascript

  • Python
def fc()
    return (1,"2",3);

v1,v2,v3 = fc();
  • Javascript
function fc(){
    return {1,"2",3}
}
let {v1,v2,v3} = fc()

Look at the Go more examples of the return value

func fc() (int, string, int) {
    return 1, "2", 3
}

In the statically typed help, the entire method is relatively clearer, and more return value Go will often encounter program, especially when the method to be returned at the same time a wrong time.

Own method of formatting code

Go inside provides a go fmtBahrain Go environment in the future, and configure the environment variables later, you can use go fmt main.goto format the code

Upgraded version if

General use if

if 1 == 1 {
}

Why go in if you are upgrading over it

if count := 10; count < 10 {
}

The code can be if the assignment operation, in fact, can also be a function call, math.

Use go module management relies

At the time of writing this article go 1.13 has been released, the go 1.11 brings new package management mode Go Module .
In 1.11 before, I used that DEP , in this way is inseparable from the go path , that is, you need to put the project on the go path down.
Use go module can be from the Go path , you can put your project random on where you want to put.
You can refer to this article to learn Go Module .

No while loop

I have talked Go outside of the high-level computer language , the cycle will be available in two, for and the while .
Go The for loop is no parameters, as follows

for {
}

This time is a cycle of death, if the arguments, then, and most of the language is the same

for i := 1; i < 10; i++ {

}

No ++ i, - i

The words seem harmless.

No ternary operator

No ternary operator that looks very uncomfortable, for me this lazy habit of people, means that use long if else instead, which is to write the code point of view.

Guess you like

Origin www.cnblogs.com/97jay/p/11608390.html