go

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Go

Go

A simple, fast, and concurrent language developed by Google.
由Google开发的一门简洁、快速且支持并发的语言。

When to Use

适用场景

  • Microservices / Cloud-native apps
  • Network tools (servers, proxies)
  • High-performance systems
  • CLIs
  • 微服务 / 云原生应用
  • 网络工具(服务器、代理)
  • 高性能系统
  • CLIs

Quick Start

快速开始

go
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")

    ch := make(chan string)
    go func() {
        ch <- "from goroutine"
    }()

    msg := <-ch
    fmt.Println(msg)
}
go
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")

    ch := make(chan string)
    go func() {
        ch <- "from goroutine"
    }()

    msg := <-ch
    fmt.Println(msg)
}

Core Concepts

核心概念

Goroutines

Goroutines

Lightweight threads managed by the Go runtime.
go
go doSomething()
由Go运行时管理的轻量级线程。
go
go doSomething()

Channels

Channels

Typed conduits for communication between goroutines.
用于goroutine之间通信的类型化管道。

Interfaces

Interfaces

Implicitly implemented. If a struct has the methods, it implements the interface.
隐式实现。如果结构体拥有对应的方法,即实现了该接口。

Best Practices

最佳实践

Do:
  • Handle errors explicitly (check
    if err != nil
    )
  • Use
    gofmt
    to format code
  • Keep extensive comments for public APIs
  • Use context for cancellation and timeouts
Don't:
  • Ignore errors (using
    _
    ) blindly
  • Use panic/recover for normal error handling
  • Create large interfaces (keep them small)
建议
  • 显式处理错误(检查
    if err != nil
  • 使用
    gofmt
    格式化代码
  • 为公共API添加详细注释
  • 使用context进行取消操作和超时控制
不建议
  • 盲目忽略错误(使用
    _
  • 将panic/recover用于常规错误处理
  • 创建大型接口(保持接口精简)

References

参考资料