- go is fast, statically typed (variable types are checked by compiler), compiled language (compiled to machine code), strongly typed language(can't change var type in future)
- general purpose programming lang
- built-in testing support
- object oriented language
- if declared and not use -> error
- usually entry point is main.go
- In Go, a directory is a package, and a package can only have one function with a given name (with the exception of init(), which is a special case)
- no while loop
Here's a compact Go cheatsheet covering essential syntax, concepts, and features:
-
Package declaration: All Go files begin with
packagekeyword.package main -
Imports: Use
importto bring in packages.import "fmt"
-
Entry point: Main function to run a Go program.
func main() { fmt.Println("Hello, Go!") }
-
Declaration: Variables declared with
varor:=syntax.var x int = 10 // Explicit type var y = "Hello" // Implicit type z := true // Short declaration (inside functions only)
-
Constants: Declared with
const.const Pi = 3.14
- Primitive types:
int,float64,string,bool - Composite types:
struct,array,slice,map
-
If-Else
if x > 10 { fmt.Println("x is greater than 10") } else { fmt.Println("x is 10 or less") }
-
For Loop (the only loop in Go)
for i := 0; i < 5; i++ { fmt.Println(i) } // Range loop for i, v := range arr { fmt.Println(i, v) }
-
Switch
switch x { case 1: fmt.Println("One") case 2: fmt.Println("Two") default: fmt.Println("Other") }
-
Basic function syntax
func add(a int, b int) int { return a + b }
-
Multiple return values
func swap(x, y string) (string, string) { return y, x }
-
Named return values
func split(sum int) (x, y int) { x = sum * 4 / 9 y = sum - x return }
- Declaration and usage
var p *int i := 42 p = &i // p now points to i fmt.Println(*p) // Dereference p
-
Define and initialize
type Person struct { Name string Age int } p := Person{Name: "Alice", Age: 30}
-
Access fields
fmt.Println(p.Name)
- Method declaration
func (p Person) greet() string { return "Hello, " + p.Name }
-
Define interface
type Speaker interface { Speak() string } func (p Person) Speak() string { return "Hello, I'm " + p.Name }
-
Use interface in functions
func sayHello(s Speaker) { fmt.Println(s.Speak()) }
- Error type
func divide(a, b int) (int, error) { if b == 0 { return 0, fmt.Errorf("cannot divide by zero") } return a / b, nil }
-
Goroutine (lightweight thread)
go func() { fmt.Println("Running in a goroutine") }()
-
Channels: For safe communication between goroutines
ch := make(chan int) ch <- 5 // Send to channel x := <-ch // Receive from channel
-
Buffered channels
ch := make(chan int, 2)
-
Select statement (for multiple channel operations)
select { case msg := <-ch1: fmt.Println(msg) case msg := <-ch2: fmt.Println(msg) default: fmt.Println("No messages") }
-
Initialize new module
go mod init moduleName
-
Get packages
go get packageName
- String length:
len(str) - Append to slice:
append(slice, elem) - Delete from map:
delete(map, key)
This cheatsheet covers the essentials of Go to help you quickly recall key syntax and concepts. Let me know if you need any deeper explanations on any specific topic!