var x int = 10 | Variable declaration |
x := 10 | Short declaration |
const Pi = 3.14 | Constant |
int, int8, int16, int32, int64 | Integer types |
uint, uint8, uint16, uint32, uint64 | Unsigned integers |
float32, float64 | Floating point |
bool | Boolean (true/false) |
string | String type |
byte (uint8), rune (int32) | Byte and Unicode |
+ - * / % | Arithmetic |
== != < > <= >= | Comparison |
&& || ! | Logical |
& | ^ << >> | Bitwise |
&x | Address of x |
*p | Dereference pointer |
:= | Short variable declaration |
if cond { } | If statement |
if x := f(); x > 0 { } | If with init |
if cond { } else { } | If-else |
for i := 0; i < n; i++ { } | Classic for loop |
for cond { } | While-style loop |
for { } | Infinite loop |
for i, v := range slice { } | Range over slice |
for k, v := range map { } | Range over map |
break / continue | Loop control |
switch x { case 1: ... } | Switch statement |
switch { case x > 0: ... } | Switch without expr |
case 1, 2, 3: | Multiple values |
default: | Default case |
fallthrough | Fall to next case |
switch v := x.(type) { } | Type switch |
func name(a int, b int) int { } | Function with return |
func name(a, b int) (int, error) { } | Multiple returns |
func name(a ...int) { } | Variadic function |
func (x int, y int) (sum int) { } | Named return |
fn := func(x int) int { } | Anonymous function |
defer fn() | Defer execution |
return a, b | Return multiple values |
func (s *Struct) Method() { } | Pointer receiver |
func (s Struct) Method() { } | Value receiver |
s.Method() | Call method |
var arr [5]int | Array (fixed size) |
arr := [...]int{1, 2, 3} | Array literal |
slice := []int{1, 2, 3} | Slice literal |
slice := make([]int, len, cap) | Make slice |
slice[1:3] | Slice slice |
append(slice, elem) | Append to slice |
len(slice) / cap(slice) | Length / capacity |
copy(dst, src) | Copy slice |
m := make(map[string]int) | Create map |
m := map[string]int{"a": 1} | Map literal |
m[key] = value | Set value |
value := m[key] | Get value |
value, ok := m[key] | Check existence |
delete(m, key) | Delete key |
len(m) | Map size |
type Name struct { field Type } | Define struct |
s := Name{field: value} | Struct literal |
s.field | Access field |
p := &Name{} | Pointer to struct |
type Name struct { Other } | Embedded struct |
`json:"name"` | Struct tag |
type Name interface { Method() } | Define interface |
interface{} | Empty interface (any) |
any | Alias for interface{} |
v.(Type) | Type assertion |
v, ok := x.(Type) | Safe type assertion |
switch v := x.(type) { } | Type switch |
go func() { }() | Start goroutine |
go functionName() | Run function as goroutine |
runtime.NumGoroutine() | Get goroutine count |
ch := make(chan int) | Unbuffered channel |
ch := make(chan int, 10) | Buffered channel |
ch <- value | Send to channel |
value := <-ch | Receive from channel |
close(ch) | Close channel |
for v := range ch { } | Range over channel |
v, ok := <-ch | Check if closed |
chan<- int | Send-only channel |
<-chan int | Receive-only channel |
select { case <-ch1: ... case ch2 <- v: ... } | Select statement |
default: | Non-blocking select |
case <-time.After(1 * time.Second): | Timeout |
var wg sync.WaitGroup | WaitGroup |
wg.Add(1) / wg.Done() / wg.Wait() | WaitGroup methods |
var mu sync.Mutex | Mutex |
mu.Lock() / mu.Unlock() | Lock/unlock |
var once sync.Once | Once (run once) |
if err != nil { return err } | Check error |
errors.New("message") | Create error |
fmt.Errorf("msg: %w", err) | Wrap error |
errors.Is(err, target) | Check error type |
errors.As(err, &target) | Extract error |
errors.Unwrap(err) | Unwrap error |
panic("message") | Panic (crash) |
defer func() { recover() }() | Recover from panic |
fmt.Println() / Printf() / Sprintf() | Print functions |
strings.Split() / Join() / Contains() | String manipulation |
strconv.Itoa() / Atoi() | String conversion |
time.Now() / time.Sleep() | Time functions |
os.Open() / os.Create() / os.Remove() | File operations |
json.Marshal() / Unmarshal() | JSON encoding |
http.Get() / http.ListenAndServe() | HTTP client/server |
context.Background() / WithTimeout() | Context handling |
go run main.go | Run program |
go build | Build binary |
go test | Run tests |
go mod init module | Initialize module |
go mod tidy | Clean dependencies |
go get package | Add dependency |
go fmt | Format code |
go vet | Static analysis |
:= for short variable declarations within functionsdefer for cleanup (close files, unlock mutexes)go fmt and go vet before committing