今日已更新 80 条资讯 | 累计 20052 条内容
关于我们

Go 1.26 Cheat Sheet

lappy 2026年06月23日 20:29 1 次阅读 来源:Dev.to

This cheat sheet is based on Go 1.26. Table of Contents Program structure Variables, constants, and types Control flow Functions Arrays, slices, and maps Structs, methods, and tags Interfaces Errors Goroutines, channels, and synchronization Context Generics Iterators Testing Standard-library quick reference Tips and common mistakes Modules and packages Further reading Program structure package main // Every executable must be in package main. import ( "fmt" "os" ) func main () { fmt . Println ( "Hello, Go 1.26" ) os . Exit ( 0 ) } Every Go source file starts with a package declaration. An executable program uses package main and a main function. Group standard-library and third-party imports separately. Variables, constants, and types Variable declarations and inference // var can infer the type from an initializer. var name = "Gopher" // string var age int // No initializer: specify a type. Zero value: 0. var id int64 = 42 // Use an explicit type when int is not what you want. // Short declaration: inside functions only. city := "Tokyo" // Declare multiple variables in a block. var ( x , y int = 1 , 2 msg string ) Use var name = "Gopher" when an initializer makes the type clear. Without an initializer, write the type explicitly. A := declaration is only valid inside a function, and at least one name on its left side must be new. Types and zero values Category Types Zero value Numbers integers, floats, complex numbers 0 ( 0 + 0i for complex) Strings string "" Booleans bool false Arrays [N]T every element is T 's zero value Structs struct every field has its zero value Reference-containing types pointers, slices, maps, channels, functions, interfaces nil Defined types e.g. type UserID int the underlying type's zero value Category Types Notes Boolean bool true or false String string immutable sequence of bytes (usually UTF-8, but invalid UTF-8 is allowed) Signed integers int , int8 , int16 , int32 , int64 int is platform-sized (32 or 64 bit) Unsigned integers uint , u

本文内容来源于互联网,版权归原作者所有
查看原文