<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://tech.uvoo.io/index.php?action=history&amp;feed=atom&amp;title=Golang_Cheatsheet</id>
	<title>Golang Cheatsheet - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://tech.uvoo.io/index.php?action=history&amp;feed=atom&amp;title=Golang_Cheatsheet"/>
	<link rel="alternate" type="text/html" href="https://tech.uvoo.io/index.php?title=Golang_Cheatsheet&amp;action=history"/>
	<updated>2026-04-04T14:06:41Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.2</generator>
	<entry>
		<id>https://tech.uvoo.io/index.php?title=Golang_Cheatsheet&amp;diff=1261&amp;oldid=prev</id>
		<title>Busk: Created page with &quot;Shameless rip from https://devhints.io/go ``` Introduction A tour of Go (tour.golang.org) Go repl (repl.it) Golang wiki (github.com) Hello world hello.go package main  import...&quot;</title>
		<link rel="alternate" type="text/html" href="https://tech.uvoo.io/index.php?title=Golang_Cheatsheet&amp;diff=1261&amp;oldid=prev"/>
		<updated>2021-02-21T23:40:59Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Shameless rip from https://devhints.io/go ``` Introduction A tour of Go (tour.golang.org) Go repl (repl.it) Golang wiki (github.com) Hello world hello.go package main  import...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Shameless rip from https://devhints.io/go&lt;br /&gt;
```&lt;br /&gt;
Introduction&lt;br /&gt;
A tour of Go&lt;br /&gt;
(tour.golang.org)&lt;br /&gt;
Go repl&lt;br /&gt;
(repl.it)&lt;br /&gt;
Golang wiki&lt;br /&gt;
(github.com)&lt;br /&gt;
Hello world&lt;br /&gt;
hello.go&lt;br /&gt;
package main&lt;br /&gt;
&lt;br /&gt;
import &amp;quot;fmt&amp;quot;&lt;br /&gt;
&lt;br /&gt;
func main() {&lt;br /&gt;
  message := greetMe(&amp;quot;world&amp;quot;)&lt;br /&gt;
  fmt.Println(message)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
func greetMe(name string) string {&lt;br /&gt;
  return &amp;quot;Hello, &amp;quot; + name + &amp;quot;!&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
$ go build&lt;br /&gt;
Or try it out in the Go repl, or A Tour of Go.&lt;br /&gt;
&lt;br /&gt;
Variables&lt;br /&gt;
Variable declaration&lt;br /&gt;
var msg string&lt;br /&gt;
msg = &amp;quot;Hello&amp;quot;&lt;br /&gt;
Shortcut of above (Infers type)&lt;br /&gt;
msg := &amp;quot;Hello&amp;quot;&lt;br /&gt;
Constants&lt;br /&gt;
const Phi = 1.618&lt;br /&gt;
Constants can be character, string, boolean, or numeric values.&lt;br /&gt;
&lt;br /&gt;
See: Constants&lt;br /&gt;
&lt;br /&gt;
#Basic types&lt;br /&gt;
Strings&lt;br /&gt;
str := &amp;quot;Hello&amp;quot;&lt;br /&gt;
str := `Multiline&lt;br /&gt;
string`&lt;br /&gt;
Strings are of type string.&lt;br /&gt;
&lt;br /&gt;
Numbers&lt;br /&gt;
Typical types&lt;br /&gt;
num := 3          // int&lt;br /&gt;
num := 3.         // float64&lt;br /&gt;
num := 3 + 4i     // complex128&lt;br /&gt;
num := byte('a')  // byte (alias for uint8)&lt;br /&gt;
Other types&lt;br /&gt;
var u uint = 7        // uint (unsigned)&lt;br /&gt;
var p float32 = 22.7  // 32-bit float&lt;br /&gt;
Arrays&lt;br /&gt;
// var numbers [5]int&lt;br /&gt;
numbers := [...]int{0, 0, 0, 0, 0}&lt;br /&gt;
Arrays have a fixed size.&lt;br /&gt;
&lt;br /&gt;
Slices&lt;br /&gt;
slice := []int{2, 3, 4}&lt;br /&gt;
slice := []byte(&amp;quot;Hello&amp;quot;)&lt;br /&gt;
Slices have a dynamic size, unlike arrays.&lt;br /&gt;
&lt;br /&gt;
Pointers&lt;br /&gt;
func main () {&lt;br /&gt;
  b := *getPointer()&lt;br /&gt;
  fmt.Println(&amp;quot;Value is&amp;quot;, b)&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
func getPointer () (myPointer *int) {&lt;br /&gt;
  a := 234&lt;br /&gt;
  return &amp;amp;a&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
a := new(int)&lt;br /&gt;
*a = 234&lt;br /&gt;
 &lt;br /&gt;
Pointers point to a memory location of a variable. Go is fully garbage-collected.&lt;br /&gt;
&lt;br /&gt;
See: Pointers&lt;br /&gt;
&lt;br /&gt;
Type conversions&lt;br /&gt;
i := 2&lt;br /&gt;
f := float64(i)&lt;br /&gt;
u := uint(i)&lt;br /&gt;
See: Type conversions&lt;br /&gt;
&lt;br /&gt;
#Flow control&lt;br /&gt;
Conditional&lt;br /&gt;
if day == &amp;quot;sunday&amp;quot; || day == &amp;quot;saturday&amp;quot; {&lt;br /&gt;
  rest()&lt;br /&gt;
} else if day == &amp;quot;monday&amp;quot; &amp;amp;&amp;amp; isTired() {&lt;br /&gt;
  groan()&lt;br /&gt;
} else {&lt;br /&gt;
  work()&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
See: If&lt;br /&gt;
&lt;br /&gt;
Statements in if&lt;br /&gt;
if _, err := doThing(); err != nil {&lt;br /&gt;
  fmt.Println(&amp;quot;Uh oh&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
A condition in an if statement can be preceded with a statement before a ;. Variables declared by the statement are only in scope until the end of the if.&lt;br /&gt;
&lt;br /&gt;
See: If with a short statement&lt;br /&gt;
&lt;br /&gt;
Switch&lt;br /&gt;
switch day {&lt;br /&gt;
  case &amp;quot;sunday&amp;quot;:&lt;br /&gt;
    // cases don't &amp;quot;fall through&amp;quot; by default!&lt;br /&gt;
    fallthrough&lt;br /&gt;
&lt;br /&gt;
  case &amp;quot;saturday&amp;quot;:&lt;br /&gt;
    rest()&lt;br /&gt;
&lt;br /&gt;
  default:&lt;br /&gt;
    work()&lt;br /&gt;
}&lt;br /&gt;
See: Switch&lt;br /&gt;
&lt;br /&gt;
For loop&lt;br /&gt;
for count := 0; count &amp;lt;= 10; count++ {&lt;br /&gt;
  fmt.Println(&amp;quot;My counter is at&amp;quot;, count)&lt;br /&gt;
}&lt;br /&gt;
See: For loops&lt;br /&gt;
&lt;br /&gt;
For-Range loop&lt;br /&gt;
entry := []string{&amp;quot;Jack&amp;quot;,&amp;quot;John&amp;quot;,&amp;quot;Jones&amp;quot;}&lt;br /&gt;
for i, val := range entry {&lt;br /&gt;
  fmt.Printf(&amp;quot;At position %d, the character %s is present\n&amp;quot;, i, val)&lt;br /&gt;
}&lt;br /&gt;
See: For-Range loops&lt;br /&gt;
&lt;br /&gt;
While loop&lt;br /&gt;
n := 0&lt;br /&gt;
x := 42&lt;br /&gt;
for n != x {&lt;br /&gt;
  n := guess()&lt;br /&gt;
}&lt;br /&gt;
See: Go’s “while”&lt;br /&gt;
&lt;br /&gt;
#Functions&lt;br /&gt;
Lambdas&lt;br /&gt;
myfunc := func() bool {&lt;br /&gt;
  return x &amp;gt; 10000&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Functions are first class objects.&lt;br /&gt;
&lt;br /&gt;
Multiple return types&lt;br /&gt;
a, b := getMessage()&lt;br /&gt;
func getMessage() (a string, b string) {&lt;br /&gt;
  return &amp;quot;Hello&amp;quot;, &amp;quot;World&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Named return values&lt;br /&gt;
func split(sum int) (x, y int) {&lt;br /&gt;
  x = sum * 4 / 9&lt;br /&gt;
  y = sum - x&lt;br /&gt;
  return&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
By defining the return value names in the signature, a return (no args) will return variables with those names.&lt;br /&gt;
&lt;br /&gt;
See: Named return values&lt;br /&gt;
&lt;br /&gt;
#Packages&lt;br /&gt;
Importing&lt;br /&gt;
import &amp;quot;fmt&amp;quot;&lt;br /&gt;
import &amp;quot;math/rand&amp;quot;&lt;br /&gt;
import (&lt;br /&gt;
  &amp;quot;fmt&amp;quot;        // gives fmt.Println&lt;br /&gt;
  &amp;quot;math/rand&amp;quot;  // gives rand.Intn&lt;br /&gt;
)&lt;br /&gt;
Both are the same.&lt;br /&gt;
&lt;br /&gt;
See: Importing&lt;br /&gt;
&lt;br /&gt;
Aliases&lt;br /&gt;
import r &amp;quot;math/rand&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
r.Intn()&lt;br /&gt;
Exporting names&lt;br /&gt;
func Hello () {&lt;br /&gt;
  ···&lt;br /&gt;
}&lt;br /&gt;
Exported names begin with capital letters.&lt;br /&gt;
&lt;br /&gt;
See: Exported names&lt;br /&gt;
&lt;br /&gt;
Packages&lt;br /&gt;
package hello&lt;br /&gt;
Every package file has to start with package.&lt;br /&gt;
&lt;br /&gt;
#Concurrency&lt;br /&gt;
Goroutines&lt;br /&gt;
func main() {&lt;br /&gt;
  // A &amp;quot;channel&amp;quot;&lt;br /&gt;
  ch := make(chan string)&lt;br /&gt;
&lt;br /&gt;
  // Start concurrent routines&lt;br /&gt;
  go push(&amp;quot;Moe&amp;quot;, ch)&lt;br /&gt;
  go push(&amp;quot;Larry&amp;quot;, ch)&lt;br /&gt;
  go push(&amp;quot;Curly&amp;quot;, ch)&lt;br /&gt;
&lt;br /&gt;
  // Read 3 results&lt;br /&gt;
  // (Since our goroutines are concurrent,&lt;br /&gt;
  // the order isn't guaranteed!)&lt;br /&gt;
  fmt.Println(&amp;lt;-ch, &amp;lt;-ch, &amp;lt;-ch)&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
func push(name string, ch chan string) {&lt;br /&gt;
  msg := &amp;quot;Hey, &amp;quot; + name&lt;br /&gt;
  ch &amp;lt;- msg&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Channels are concurrency-safe communication objects, used in goroutines.&lt;br /&gt;
&lt;br /&gt;
See: Goroutines, Channels&lt;br /&gt;
&lt;br /&gt;
Buffered channels&lt;br /&gt;
ch := make(chan int, 2)&lt;br /&gt;
ch &amp;lt;- 1&lt;br /&gt;
ch &amp;lt;- 2&lt;br /&gt;
ch &amp;lt;- 3&lt;br /&gt;
// fatal error:&lt;br /&gt;
// all goroutines are asleep - deadlock!&lt;br /&gt;
 &lt;br /&gt;
Buffered channels limit the amount of messages it can keep.&lt;br /&gt;
&lt;br /&gt;
See: Buffered channels&lt;br /&gt;
&lt;br /&gt;
Closing channels&lt;br /&gt;
Closes a channel&lt;br /&gt;
ch &amp;lt;- 1&lt;br /&gt;
ch &amp;lt;- 2&lt;br /&gt;
ch &amp;lt;- 3&lt;br /&gt;
close(ch)&lt;br /&gt;
 &lt;br /&gt;
Iterates across a channel until its closed&lt;br /&gt;
for i := range ch {&lt;br /&gt;
  ···&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Closed if ok == false&lt;br /&gt;
v, ok := &amp;lt;- ch&lt;br /&gt;
See: Range and close&lt;br /&gt;
&lt;br /&gt;
WaitGroup&lt;br /&gt;
import &amp;quot;sync&amp;quot;&lt;br /&gt;
&lt;br /&gt;
func main() {&lt;br /&gt;
  var wg sync.WaitGroup&lt;br /&gt;
  &lt;br /&gt;
  for _, item := range itemList {&lt;br /&gt;
    // Increment WaitGroup Counter&lt;br /&gt;
    wg.Add(1)&lt;br /&gt;
    go doOperation(item)&lt;br /&gt;
  }&lt;br /&gt;
  // Wait for goroutines to finish&lt;br /&gt;
  wg.Wait()&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
func doOperation(item string) {&lt;br /&gt;
  defer wg.Done()&lt;br /&gt;
  // do operation on item&lt;br /&gt;
  // ...&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
A WaitGroup waits for a collection of goroutines to finish. The main goroutine calls Add to set the number of goroutines to wait for. The goroutine calls wg.Done() when it finishes. See: WaitGroup&lt;br /&gt;
&lt;br /&gt;
#Error control&lt;br /&gt;
Defer&lt;br /&gt;
func main() {&lt;br /&gt;
  defer fmt.Println(&amp;quot;Done&amp;quot;)&lt;br /&gt;
  fmt.Println(&amp;quot;Working...&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Defers running a function until the surrounding function returns. The arguments are evaluated immediately, but the function call is not ran until later.&lt;br /&gt;
&lt;br /&gt;
See: Defer, panic and recover&lt;br /&gt;
&lt;br /&gt;
Deferring functions&lt;br /&gt;
func main() {&lt;br /&gt;
  defer func() {&lt;br /&gt;
    fmt.Println(&amp;quot;Done&amp;quot;)&lt;br /&gt;
  }()&lt;br /&gt;
  fmt.Println(&amp;quot;Working...&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
Lambdas are better suited for defer blocks.&lt;br /&gt;
&lt;br /&gt;
func main() {&lt;br /&gt;
  var d = int64(0)&lt;br /&gt;
  defer func(d *int64) {&lt;br /&gt;
    fmt.Printf(&amp;quot;&amp;amp; %v Unix Sec\n&amp;quot;, *d)&lt;br /&gt;
  }(&amp;amp;d)&lt;br /&gt;
  fmt.Print(&amp;quot;Done &amp;quot;)&lt;br /&gt;
  d = time.Now().Unix()&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
The defer func uses current value of d, unless we use a pointer to get final value at end of main.&lt;br /&gt;
&lt;br /&gt;
#Structs&lt;br /&gt;
Defining&lt;br /&gt;
type Vertex struct {&lt;br /&gt;
  X int&lt;br /&gt;
  Y int&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
func main() {&lt;br /&gt;
  v := Vertex{1, 2}&lt;br /&gt;
  v.X = 4&lt;br /&gt;
  fmt.Println(v.X, v.Y)&lt;br /&gt;
}&lt;br /&gt;
See: Structs&lt;br /&gt;
&lt;br /&gt;
Literals&lt;br /&gt;
v := Vertex{X: 1, Y: 2}&lt;br /&gt;
// Field names can be omitted&lt;br /&gt;
v := Vertex{1, 2}&lt;br /&gt;
// Y is implicit&lt;br /&gt;
v := Vertex{X: 1}&lt;br /&gt;
You can also put field names.&lt;br /&gt;
&lt;br /&gt;
Pointers to structs&lt;br /&gt;
v := &amp;amp;Vertex{1, 2}&lt;br /&gt;
v.X = 2&lt;br /&gt;
Doing v.X is the same as doing (*v).X, when v is a pointer.&lt;br /&gt;
&lt;br /&gt;
#Methods&lt;br /&gt;
Receivers&lt;br /&gt;
type Vertex struct {&lt;br /&gt;
  X, Y float64&lt;br /&gt;
}&lt;br /&gt;
func (v Vertex) Abs() float64 {&lt;br /&gt;
  return math.Sqrt(v.X * v.X + v.Y * v.Y)&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
v := Vertex{1, 2}&lt;br /&gt;
v.Abs()&lt;br /&gt;
There are no classes, but you can define functions with receivers.&lt;br /&gt;
&lt;br /&gt;
See: Methods&lt;br /&gt;
&lt;br /&gt;
Mutation&lt;br /&gt;
func (v *Vertex) Scale(f float64) {&lt;br /&gt;
  v.X = v.X * f&lt;br /&gt;
  v.Y = v.Y * f&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
v := Vertex{6, 12}&lt;br /&gt;
v.Scale(0.5)&lt;br /&gt;
// `v` is updated&lt;br /&gt;
By defining your receiver as a pointer (*Vertex), you can do mutations.&lt;br /&gt;
&lt;br /&gt;
See: Pointer receivers&lt;br /&gt;
&lt;br /&gt;
#Interfaces&lt;br /&gt;
A basic interface&lt;br /&gt;
type Shape interface {&lt;br /&gt;
  Area() float64&lt;br /&gt;
  Perimeter() float64&lt;br /&gt;
}&lt;br /&gt;
Struct&lt;br /&gt;
type Rectangle struct {&lt;br /&gt;
  Length, Width float64&lt;br /&gt;
}&lt;br /&gt;
Struct Rectangle implicitly implements interface Shape by implementing all of its methods.&lt;br /&gt;
&lt;br /&gt;
Methods&lt;br /&gt;
func (r Rectangle) Area() float64 {&lt;br /&gt;
  return r.Length * r.Width&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
func (r Rectangle) Perimeter() float64 {&lt;br /&gt;
  return 2 * (r.Length + r.Width)&lt;br /&gt;
}&lt;br /&gt;
The methods defined in Shape are implemented in Rectangle.&lt;br /&gt;
&lt;br /&gt;
Interface example&lt;br /&gt;
func main() {&lt;br /&gt;
  var r Shape = Rectangle{Length: 3, Width: 4}&lt;br /&gt;
  fmt.Printf(&amp;quot;Type of r: %T, Area: %v, Perimeter: %v.&amp;quot;, r, r.Area(), r.Perimeter())&lt;br /&gt;
}&lt;br /&gt;
```&lt;/div&gt;</summary>
		<author><name>Busk</name></author>
	</entry>
</feed>