介绍

Go是一种静态强类型编译型并发型并且具有垃圾回收的编程语言

资源

安装

基础使用

  • 以下说明以Linux为例,Windows同理
  1. 创建文件夹并进入:mkdir goTest

  2. 初始化:go mod init test1,此时会生成go.mod文件,内容如下

    image-20221107104635882

  3. 创建一个新文件:vim main.go,内容如下

    1
    2
    3
    4
    5
    6
    7
    package main

    import "fmt"

    func main() {
    fmt.Println("hello, world")
    }
  4. 运行:go run main.go

    image-20221107104658600

设计思想

  • 为什么Go的声明不同于C风格的声明?

  • Defer, Panic, and Recover

    • Defer:更好的回收资源(当存在异常退出时)

    • Panic, and Recover:发生错误时,能够正常退出并给出错误位置

      The convention in the Go libraries is that even when a package uses panic internally, its external API still presents explicit error return values.

    • Defer, Panic, and Recover