2017年12月24日 星期日

Getting Start in Go -- part 1

Many people want to learn Go. But how to start? That is a big problem.
Maybe you already heard a lot like: Go is good at concurrency with go-routine, channel brabrabra, but I think those things won't be most important things in Go.

I think the most important thing in Go is: Simple. Keep this in mind.
Why did I say that? Think about
public static void main(String[] Args)
Should we really need such like this?
With a link, I hope you can rethink the problem.
That twitter shows an unreadable error message from C++ compiler.
Why simple is the point, consider python. How quick is its development?
So I will tell you some good part of Go. And what's are you should avoid.

@ package system
import (
    "os"
    "fmt"
)
import something in Go is simple. Just with some string, but what rule them use?
It managed by path.
You need to set $GOPATH in your system at the beginning. And install process should set a $GOROOT, and they are the package location.
For example, if you have a GitHub project.
You should place it under: $GOPATH/src/github.com/your-account/project
[Good part]: In fact, because of mapping rule about domain & Go package system. We can help different people run up the project, without complex setting script(as you can see in C++ project, and more language can have)
ps. This benefit also on npm, cargo, nimble these package manager with a good package dependencies document.
Whatever, that is the way how Go deal with this problem.
After import, you will get a package name, that is final stuff of the path string
For example, import "github.com/dannypsnl/redux" will get redux.(My GitHub project, XD)
[Good part]: If you don't like the project name(maybe that is too long), or both packages have the same name, you can rename it.
import (
    "os"
    "fmt"
    rdx "github.com/dannypsnl/redux"
)
In this case, you will get rdx, instead of redux.

[Bad part]: Now, the problem comes, if you need to use a certain version of a package, but another project depended on another version of this package. How to solve it?
In fact, we don't have a good solution, it's also is the biggest problem in Go.
My way is to make sure the package I am using is using git to do version control.
Then write a build script for my project, check out the correct tag I want to use.
Not perfect, but work.

@ Variable
In Go, we have four-way to define a var.
func main() {
    var a int
    var a int = 1
    var a = 1
    a := 1
}
[!Warning]: you can not declared a four time in actual program.
1. define without an initializer, then go will init it for you.
2. define var with type and initializer
3. only initializer
4. short def
[Important]: short def can only using in function
[Good part]: I prefer to use short define in function, that is like python. And the better thing is you know that is definition or assignment.
[Bad part]: First way have a problem, in user-defined type, it usually work not good.
My suggestion doesn't use it.
The Second way is not good when the type can be easy for sure. But useful when it doesn't.
Third way useful when the type can easy to sure.

@ Loop
Go only have a loop syntax, that is "for".
> forever: 
for {
    // statements
}
Just without any condition.
> traditional for:
for i:=0; i<10; i++ {
    // statements
}
As you can find in C. [Important]: i++ is a statement, cannot be expression
> enumerate loop:
list := []int{ 1, 2, 3 }
for i, v := range list {
    // statements
}
i is index, v is value of list
If you enumerate map, then that will be key, value.
> while loop:
for ;$condition; {
    // statements
}
Put condition(i < 10) only is while loop.

@ gofmt
Go required the same format of the file.
[Good part]: Easy to find out the syntax error. The team works become easier at here.
[Bad part]: Maybe you just hate being controlled.

I will talk about others things next time. See you.

沒有留言:

張貼留言