当前位置:网站首页>The way to learn go (I) the basic introduction of go to the first HelloWorld

The way to learn go (I) the basic introduction of go to the first HelloWorld

2022-07-06 07:18:00 Tiger up

1.go Installation and environment variable configuration :

Get into https://golang.google.cn/dl/
choice go Install the version and system of
Set up after installation GOHOME, And will GOHOME/bin Add to environment variables
Enter... When finished go Verify that the installation was successful

2.go The advantages of :

1. Bring their own gc.

2. Static compilation , After compiling , Throw the server to run directly .

3. Simple thoughts , No inheritance , polymorphic , Class etc. .

4. Rich libraries and detailed development documents .

5. The syntax layer supports concurrency , And those with synchronous concurrency channel type , Make concurrent development very convenient .

6 Simple grammar , Improve development efficiency , At the same time, it improves the readability and maintainability of the code .

7. Super simple cross compilation , Just change the environment variable .

3.go Characteristics of :

1. Automatic immediate recycling .
2. Richer built-in types .
3. Function returns multiple values .
4. Error handling .
5. Anonymous functions and closures .
6. Types and interfaces .
7. Concurrent programming .
8. Reflection .
9. Language interactivity

4.go file name

be-all go The source code is based on “.go” ending .

5.go Language naming of :

1.Go Function of 、 Variable 、 Constant 、 Custom type 、 package (package) The following rules apply to the naming of :

1) The first character can be arbitrary Unicode Character or underscore
2) The remaining characters can be Unicode character 、 Underline 、 Numbers
3) There is no limit to the length of characters

2.Go Only 25 Key words

break default func interface select case
defer go map struct chan else
goto package switch const fallthrough if
range type continue for import return
var

3.Go also 37 A reserved word

Constants: true false iota nil Types: int int8 int16 int32
int64
uint uint8 uint16 uint32 uint64 uintptr
float32 float64 complex128 complex64
bool byte rune string error Functions: make len cap new append copy close delete
complex real imag
panic recover

4. visibility :

1) Declared inside a function , Is the local value of the function , similar private
2) Declared outside the function , Is visible to the current package ( All in the package .go All files are visible ) Global value of , similar protect
3) Declared outside the function and capitalized is the global value visible to all packages , similar public

6.Go Language statement :
There are four main ways to declare :

var( Declare variables ), const( declare constant ), type( Declaration type ) ,func( Declare functions )

Go The program is stored in multiple .go In file , The first line of the file is package XXX Statement , It is used to indicate which package the file belongs to (package),package It's a statement import Statement , Next comes the type , Variable , Constant , Declaration of functions .

7.Go Project construction and compilation
One Go The project mainly includes the following three directories :

src: Source code file
pkg: Package file
bin: relevant bin file

8.Go One of the first helloworld

newly build goproject
goproject Under the new src、pkg、bin
open src newly build helloworld.go, And enter the following

package main

import "fmt"

func main(){
    
	fmt.Println("hello world")
}

Command line window execution go build
Generate a helloworld Of exe file
Execute this file to print hello world
 Insert picture description here

原网站

版权声明
本文为[Tiger up]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060715428561.html