Go basics of programming (introduction and installation)

 

2018.10.07 19:41 Words 892 read 317 comments 0

Go (also known as golang [ 3] ) is Google a development of static strongly typed , compiled , and hair , and has a garbage collection function of the programming language . From Wikipedia

1. What is the Go language?

image.png

Go is a concurrency support, garbage collection and compilation systems compiled languages, aimed at creating a door has has a good balance between high performance and efficient development of dynamic languages ​​statically compiled language, a programming language.

Go language is Rob Pike and Ken Thompson in September 2007 to start the design, Go is based on the Inferno operating system development. [ 4] Go in November 2009 officially announced to become open-source project, and Linux and Mac OS X has been achieved on the platform, and later added the realization of a Windows system. (This is similar to the marmot logo Rob Pike's wife Renee French drawing)

2. Go language advantage

Value Go Language: Go in Google: software engineering for the purpose of language design

Go main features are the following:

  • 1, type-safe and memory safe
  • 2, in a very intuitive and very low cost program to achieve high concurrency
  • 3, efficient garbage collection mechanism
  • 4, fast compiler (while addressing the problem of too many C language header file)
  • 5, provides for the performance of a multicore computer program

3.Go language of architecture project

Go language working directory in the following table, according to the agreement, GoPaththe next need to create three directories

table of Contents meaning
bin Generated after storing the compiled executable file
pkg After storing the generated files to compile the package
src Source storage project

GOPATH is your working directory, GOROOT your installation directory.

The overall structure of the program as shown below:

image.png

4.Go language development environment

This tutorial uses go1.9and goland 2018.1development environment, we must first learn to environment configuration (not talked about here, it is relatively simple), and then begin a tour of the development of Go!

1. Create a new HelloWord.gofile

Right in the new time will be two file formats:

image.png

Empty fileAfter the new file is empty, then Simple Applicationthe new will mainsample code function.

package main
import "fmt"
func main() { fmt.Print("hello,world!") } 

Go the general structure of the program:

  • 1, Go through the program packageorganized;
  • 2, only the packagename mainof the package may include a main function, and only an executable program has a main package;
  • 3, through importto the main package introducing other non-keyword;
  • 4, by constdefinition constant keyword;
  • 5, by using an external function body varto declare global variables and assign keywords;
  • 6, by typeor interface (interface) to a statement structure (struct) keywords;
  • 7, through functo declare the function key.

Results are as follows:

image.png

2. Basic type

The basic type of language go as follows:

image.png
image.png
image.png

Examples are as follows:

package main
import (
    "fmt"
    "strconv"
)
func main() { //声明和赋值 var a int = 65 var c int = 75 //简化赋值和显示强制转换 b:= string(a) //转换为数字 d:= strconv.Itoa(c) fmt.Println(b) fmt.Println(d) } 

The results are as follows:

image.png

Because the string () said it will convert the data to text format, nature and therefore anything stored in the computer is digital, so this function is natural to think that what we need is a text representation of 65 A.

3. Functions

Call the function as follows: References Go Guide Chinese version

//func 方法名  形式参数  返回参数类型
func add(x int,y int) int { return x+y } //func add(x ,y int) int { // return x+y //} func main() { fmt.Print(add(42,13)) } 

4. Back multivalued

package main
import "fmt"
func main() { a,b:=swap("hello","world") fmt.Print(a,b) } func swap(a string, b string) (string,string) { //这个可能是交换程序了,甚至不需要不需要一个临时变量,但是这个内部是怎么做的呢? return b,a } 

Q: go language swap method is how to do it?

5. Use of the variables and variable short

package main
import "fmt"
var i,j int =1,2 func main() { var c, python, java = true,false,"no" //短变量的声明和赋值 k:=3 fmt.Print(i,j,c,python, java,k) } 

6. The types of the initial value

package main
import "fmt"
func main() { var i int var f float64 var b bool var s string fmt.Printf("%v %v %v %q\n", i, f, b, s) } 
image.png

7. Type conversion (conversion must be displayed, not implicit conversions in Go)

package main
import (
    "math"
    "fmt"
)
func main() { var x, y int = 3, 4 //将xy的值强制类型转换为float64类型 var f float64 = math.Sqrt(float64(x*x+y*y)) //unit无符号整数 var z uint = uint(f) fmt.Print(x,f,z) } 

5. References

https://go-zh.org/doc/

Guess you like

Origin www.cnblogs.com/fengff/p/11447296.html