Introduction to the Go language and writing your first Go program

1. Introduction

1. New challenges in software development

  1. Multi-core hardware architecture
  2. Ultra-large-scale distributed computing cluster
  3. Unprecedented development scale and update speed of web development.

BAT, Didi and other companies are using it, and Zhihu abandoned Python and switched to Go. Docker and K8s are also used, also known as cloud development languages. Another blockchain development language.

2. Features that developers like

  1. Simple, 25 keywords
  2. Built-in concurrency support, garbage collection
  3. Efficient, compiled static language
  4. Productivity, clear introduction to dependency management, unique interface type design, programming constraints. Refer to the iOS operation to chop off a lot. From the avenue to the simplicity.

3. Misunderstanding

Shared memory concurrency control ignores the CSP concurrency mechanism of the Go language itself. Furthermore, there are pitfalls caused by Java's inelegance.

4. Google installation and official documentation

2. Write the first Go program

1. Development environment construction

GOPATH:

  1. This environment variable must be set before version 1.8
  2. After version 1.8 (including 1.8), if there is no setting, the default value is used.
  • Defaults to 0 on Unix $HOME/goand 0 on Windows %USERPROFILE%/go.
  • On Mac, GOPATH can ~/.bash_profilebe set by modifying .

2. Write a Go program

// 查看 Go 版本
go version

// 创建 Go 学习的总目录 d:\Gocode
// src 是源码路径,在下面建立 ch1/main/hello_world.go,记得 helloworld 文件拓展名记得要写 go
package main
import "fmt"

func  main() {
 fmt.Println("Hello World") 
}



//在 cmd 里面:

>d:
>cd d:\Gocode\src\ch1\main
// 运行代码
go run hello_world.go
// 编译代码,会生成一个 hello_world 的二进制文件,执行这个二进制文件也会直接回显出来 Hello World
go build hello_world.go

// Go 在默认情况下都会使用静态连接,编译完的 Go 程序是一个独立的二进制文件,有很好的便携性。可以拷贝到不同的机器上运行

3. Basic program structure

package main // 包,表明代码所在的模块(包)
import "fmt" // 引入的代码依赖

// 功能实现
func main() {
 fmt.Println("Hello World") 

4. Application entrance

  1. Must be maina package: package main(The declaration of package must be main, but the directory name does not need to be main)
// 新建一个目录`\Gocode\src\ch1\hello`,把前面的`hello_world.go`文件复制过去,依然执行正常
> go run hello_world.go  
Hello World

//但如果把`hello_world.go`里面的`package main`改成`package main1`再`go run`运行就会报错

> go run hello_world.go
package command-line-arguments is not a main package
  1. Must be maina method:func main()
  2. The file name does not have to bemain.go

5. Exit return value

Differences from other major programming languages:

  1. Functions in Go maindo not support any return values
  2. os.ExitReturn status by

package main

import (
    "fmt"
    "os"
)

func main() {
    fmt.Println("Hello World")
    os.Exit(-1)
}

Output result:

> go run hello_world.go  
Hello World
exit status 255

6. Get command line parameters

Differences from other major programming languages

  1. mainThe function does not support passing parameters
  2. os.ArgsObtain command line parameters directly in the program through
package main

import (
    "fmt"
    "os"
)

func main() {
    fmt.Println(os.Args)
    fmt.Println("Hello, world!")
    os.Exit(-1)
}

Add a parameter to execute the file and output a binary command and our command line parameters.

>go run hello_world.go ddao
[C:\Users\xxxxxxxx\xxx\xxxxxxxxxx\go-build2455331386\b001\exe\hello_world.exe ddao]
Hello, world!
exit status 255
package main

import (
    "fmt"
    "os"
)

func main() {
    if len(os.Args) > 1 {
        fmt.Println("Hello, world!", os.Args[1])
    }
}
>go run hello_world.go ddao
Hello, world! ddao

Guess you like

Origin blog.csdn.net/weixin_62173811/article/details/129909300