Golang cheat sheet
Go (or Golang) is a programming language that was developed at Google in 2007 and released to the public in 2009. It is designed to be efficient, expressive, and easy to read.
Here is a cheat sheet for Go, along with explanations for each step:
- Variables and Constants:
var
keyword is used to declare variables.const
keyword is used to declare constants.Data Types:
int
is used to represent integer values.float64
is used to represent floating-point values.string
is used to represent text.bool
is used to represent boolean values (true or false).Control Flow:
if
statement is used for conditional execution.for
loop is used for repeated execution.switch
statement is used for multi-way branching.Functions:
func
keyword is used to declare a function.- Function parameters are enclosed in parentheses.
The return type of a function is specified after the parameter list.
Arrays and Slices:
- Arrays have a fixed length and their elements are of the same type.
Slices are a dynamically-sized, flexible view into the elements of an array.
Pointers:
&
operator is used to get the address of a variable.*
operator is used to declare a pointer variable and to dereference a pointer.Structs:
type
keyword is used to define a new data type.struct
keyword is used to define a struct type.Fields of a struct are accessed using dot notation.
Maps:
map
is a built-in type that associates values with keys.Keys in a map must be of the same type, and values can be of any type.
Interfaces:
interface
is a type that defines a set of methods.A type satisfies an interface if it implements all the methods of that interface.
Error Handling:
- Go uses errors as a standard way to handle exceptional situations.
The
error
type is a built-in interface type.Concurrency:
- Goroutines are lightweight threads of execution in Go.
- The
go
keyword is used to start a new goroutine. Channels are used for communication and synchronization between goroutines.
Packages:
- Go programs are organized into packages.
- The
import
keyword is used to import packages from the standard library or other packages.
Remember, this cheat sheet is just a quick reference guide. It is important to consult the official Go documentation and practice writing code to become proficient in the language.