First, share an operation that allows you to view documents locally (linux System )
1. Open command line terminal , Input godoc -http=:8000, If you want to run in the background, add a &
2. Then open the browser , Enter url :localhost:8000 You can have a good time reading the document
go What is language? I don't think I need to introduce it ~~~ Let's go to Baidu
Next I'll knock one step at a time hello who? Code
download
If you're using linux System :
sudo apt-get install golang
If it doesn't work :
sudo add-apt-repository ppa:gophers/go
sudo apt-get update
sudo apt-get install golang
If you're using windows,mac System :
Check Baidu ~~~
Execute at the terminal go version
If you fail , Let's see how to configure environment variables
by GOPATH To prepare for
GOPATH Yes Go It's a very important environment variable for me , Before setting environment variables , We need to do some preparatory work first
Create a folder where you like , You can call it whatever you want , I was built in /home/username In the directory , The name of the folder is go. Under this folder, create 3 A folder , The names are /bin,/pkg,/src./src Put the source code , Use go get On command , The source code is downloaded in this folder ./bin Storage and use go install Command compiled executable ./pkg Store the generated file after compilation ( The role of this directory is my baidu …… I don't know how to use it , After all, beginners 0.0) thus , Preparatory work completed .
Set the environment variable
stay /etc/profile perhaps ~/.profile Add these lines to the list
export GOROOT=/usr/lib/go
export GOPATH=/home/username/go( It depends here , Look where your folder is )
export PATH= P A T H : PATH: PATH:GOPATH/bin( After adding this environment variable ,go install The generated executable file can be executed anywhere 0.0)
then , Type this code into the terminal , Make the configuration work
source .profile( It depends on which file you changed , This file is only for a specific individual user , That is to say, it only works for one user )
then , Input... At the terminal go env
You can see everything go Related environment variables
Knock on the code !
stay $GOPATH/src Under the table of contents , New name is hello Folder , Create a new hello.go Source code file for
Start typing code !!!
package main
func main() {
who := “world!”
if len(os.Args[1:]) > 0 {
who = “”
for _, arg := range os.Args[1:] {
who += " " + arg
}
}
fmt.Println(“hello”, who)
}
It's hard to see the code ? Do you think blogger's code format is poisonous ? take it easy , So let's look down .
go The tool chain of language is very powerful , One of them is called goimports Tools for ( Science popularization is a tool :gorun, You can put go Language runs as a scripting language )
If you don't install it, post the install command below :
sudo apt-get install golang-golang-x-tools
Then execute the following command :
goimports -w hello.go
-w Parameter is used to write the result to a file , Instead of the output at the terminal , Take a look at the code now
package main
import (
“fmt”
“os”
)
func main() {
who := “world!”
if len(os.Args[1:]) > 0 {
who = “”
for _, arg := range os.Args[1:] {
who += " " + arg
}
}
fmt.Println(“hello”, who)
}
hohoho, I swear I didn't touch the source file ~~~
gofmt There's also the ability to normalize code , It's just goimports Tools are more powerful , You can automatically import the packages you need (Go Language is delicate , If the package is imported but not used or the package used is not imported, the compilation will fail )
function
Get into hello Catalog , Input in the terminal go install( perhaps go install hello It works in any directory )
Get into /bin Catalog , You'll notice that there's an extra name hello The file of , Input in the terminal hello
~$ hello
hello world!
Input hello Go language
~$ hello Go language
hello Go language
analysis
Now let's parse this program .
First of all to see package main
stay Go In language , All code must be subordinate to the package , every last Go Language programs must contain a main Bag and a main() function ,main() Function is the entry to the whole program , First executed . Actually ,Go Language may also contain init() function , Precede main() perform , Used to initialize packages . It should be noted that , There is no naming conflict between package name and function name .
Go The processing unit of the language is the package, not the file , stay Go The language compiler looks like , As long as the beginning of the package is the same , Then it belongs to the same package , It's like putting it all in one file , It's hard to understand, right ~~~ It's like sugar gourd , Although there are many Hawthorn in it , But every hawthorn is part of this sugar gourd . Because of this characteristic , We can split the package into any number of source code files .
import I don't need to introduce it
Go Language doesn't need ;( Unless you enter multiple statements on the same line ),if and for Statements don't need to be (), Semicolons are added automatically by the compiler .
Go The functions and methods of language are composed of func Keyword definition .
You may see := This weird expression , This expression does two things , Declaration and assignment , There is no need to declare that the variable type does not represent Go Language is a weakly typed language ,Go Language is a strong type of language , You don't need to declare a type because Go The type is derived from the initialization value .:= Can only be used in functions , If you declare package level variables , have access to var keyword ,var name = ??? perhaps var name type. And then there's who Why don't variables :=, Because once used who:="" This expression , It's equivalent to if A new statement named who The variable of , The life cycle is if Inside the statement , It's a local variable .
stay for In circulation , Used range keyword , Each loop returns keys and values , Values are stored in arg in , It's the key .,arg It's a strange expression , This is because Go Language does not allow you to declare variables without using , And we really don't need range Return key , So use _ This thing , It's like a garbage dump .
os.Args It's a string Type of slice (slice) and python almost
All right. , That's it -_-