Skip to content

tomiis4/GoLang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Cheatsheet

File

Run file

go run file.go

Generate exe file

go build file.go

go.mod

go mod init <module-url>

Hello world

// name of package
package main

// import package
import "fmt"

// main function where will all code is executed
func main() {
    fmt.Println("Hello, world!")
}

Importing packages

// import one package
import "package"

// import multiple packages
import (
    "package1"
    "package2"
)

// import "package" as <name>
import name "package"

Variables

// variables
foo := "this is string"
foo, bar := 69, 4.20

var foo int8 = 69
var (
    foo = 4.20
    foo float32 = 4.20
)


// constants (starts with capital letter)
const Foo = 4.20
const Foo float32 = 4.20
const (
    Foo = 4.20
    Foo float32 = 4.20
)


// slices
foo := []<type>{}
foo := [][]<type>{} // 2D slice

// fixed size
foo := [...]<type>{}
foo := [6]<type>{}



/*
Types: 
    bool                = %t = true, false
    int, 8, 16, 32, 64  = %d = number in range of x bits, can be negative
    uint, 8, 16, 32, 64 = %d = number in range of x bits, can't be negative
    float32, 64         = %g = decimal numbers
    string              = %s = string
    byte                = %d = byte value
    rune                = %d = Unicode
    complex32, 64       = %g = complex numbers with real and imaginary parts
*/

Structs

// define struct
type Struct struct {
    x <type>
    y <type>
}

// initialize, method 1
foo := Struct{
    x: <value>
    y: <value>
}

// initialize, method 2
foo := Struct{}

foo.x = <value>
foo.y = <value>

Map

// define empty map
maps := map[<key-type>]<value-type>{}

// define map with values
maps := map[string]int{
    "bar": 10,
    "foo": 5,
}

// add new key
map[<key>] = <value>

// get value
value := map[<key>]

// check if value exists (ok = bool)
value, ok := map[<key>]

// delete key and value using key
delete(map, <key>)

Interface

// create interface
type Interface interface {
    func() int // return type is optional
}

// create struct
type Struct struct {}

// create method for struct
func (n Struct) method1() {
    // ...
}

// function which take interface as argument
func myFunc(x Interface) {
    x.method1()
}

// works for each struct which have same methods as interface
myFunc(Struct{}) 

Functions

// declare function
func name() {
    // ...
}

// return
func name() <type> { return x }
func name() (<type>, <type>) { return x, y }
func name() (x <type>) {
    x := <value>
    return // same as `return x`
}

// parameters 
func name(param1 <type>) {  }
func name(param1, param2 <type>) {  } // param1 must have same type as param2

// anonymous function
func() {
    // ...
}()

// method function
func (a <type>) name() {  }
x.name()

Generics

// best practice to name T or S

// create function
func name[T any](param T) {  } // v1
func name[T interface{}](param T) {  } // v2

// call function, `[type]` is optional
name[<type>](param)

Logic Statements

If/else

if statement {
    // ...
} else if statement2 {
    // ...
} else {
    // ...
}

Switch/case

switch value {
case x:
    // ...
case y:
    // ...
default:
    // ...
}

Loop

For-I

for i := 0; i < 5; i++ {
    // ...
}

For-In

for index, value := range slice {
    // ...
}

While

for {
    if statement {
        break
    }
}

Converting

import "strconv"

// str -> int
num, err := strconv.Atoi( <string> )

// int -> str
str := strconv.Itoa( <number> )

// number -> int8, 16, 32, 64
num := int<bit>( <number> )

// number -> uint8, 16, 32, 64
num := uint<bit>( <number> )

// number -> float32, float64
num := float<bit>( <number> )

// string -> byte
bytes := []byte( <string> )

// byte -> string
string( bytes )

Build-In Functions

Append

<slice> = append(<slice>, <value>)

Length

// slice
slice := []uint8{1,2,3,4}
sliceLength := len(slice) // 4

// string
str := "hello"
strLength := len(str) // 5

// map
maps := map[string]int{ "x":10, "y":15 }
mapsLength := len(maps) // 2

Panic

// make runtime error and stops the program
panic( <message> )

Copy

copy(<slice-to>, <slice-from>)

Clear

// SLICE: after clear = {0,0}
nums := []int8{69, 32}
clear(nums)

// MAP: after clear = map[]
maps := map[string]int8{
    "foo": 5,
    "bar": 10,
}
clear(maps)

Max/Min

// get biggest number
max(5,1,3,4) // 5

// get smallest number
max(5,1,3,4) // 1

Pointers

// create new pointer and allocate memory
pointer := new(<type>)

// change data from address/pointer
*pointer = <value>

// read data from address/pointer
*pointer

// get address from variable
&variable

Goroutines

// create thread
go <function()>


// wait until thread finish
import "sync"

var wg sync.WaitGroup

wg.Add(1) // number of goroutines to wait for
go myFunction() // function needs to contain `defer wg.Done()`
wg.Wait()

Channels

// create channel
ch := make(chan <type>)

// send to channel
ch <- <value>

// receive from channel
received := <-ch

// close channel
close(ch)

// check if channel was closed
received, ok := <-ch // ok = bool

Unit Testing

# run test by:
go test
// main.go
package main

func abs(number int) int {
    if number > 0 { return number }
    return number * -1
}

// main_test.go
// test files must end by `_test.go`
package main

import "testing"

// starts with capital letter
func TestAbs(t *testing.T) {
    if abs(-1) < 0 {
        t.Error("Negative value was found in abs() with", -1)
    }
}

External file

// create go.mod if you don't have one
go mod init <link-to-module> // for this we will use 'modules'

// folder structure
|- go.mod
|- main.go
|
|- example
  |- second.go

// main.go
package main
import "modules/example"

func main() {
    example.Foo()
}

// second.go
package example

func Foo() {
    // ...
}

Remote packages

Install packages

go get <link-to-module>

Import packages

import "<link-to-module>"

Packages

fmt

Print content

import "fmt"

// print on new line, variables using next argument
fmt.Println(...)

// print on same line, variables using format
fmt.Printf(...)

Get user input

import "fmt"

var variable <type>
fmt.Scanf("%<format>", &variable)

Format string

import "fmt"

fmt.Sprintf("%<format> %<format>", <variable>, <variable>)

io/ioutil

Read file content

import "io/ioutil"

// read content from `./file`
data, err := ioutil.ReadFile("./file")

regexp

Split string using regex

import "regexp"

// regex pattern for end of the line
pattern := `\r?\n` 
regexCompiled := regexp.MustCompile(pattern)

// split and save it to variable data
data := regexCompiled.Split("string", -1) // param1=string, param2=how many time do action 

json

// unmarshal (JSON -> structs)
type Struct stuct {
    x string // same as JSON key
    y string `json:"SomeKey"`  // different as JSON key
}
var structVar Struct
json.Unmarshal([]byte(<string>), &structVar)


// marshal (structs -> JSON)
type Struct struct {
    x string `json:"SomeKey"`
}
structVar := Struct{
    x: "y"
}
data, err := json.Marshal(&structVar) // use string(data)

net/http

Simple http server
import (
    "net/http"
    "log"
)

func main() {
    http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
        // request on: http://127.0.0.1:<port>/bar
    })

    port := "8080"
    err := http.ListenAndServe(":" + port, nil)

    if err != nil {
        log.Fatal(err)
    }
}

HTTP Requests

import "net/http"

// GET request
resp, err := http.Get(url)
defer resp.Body.Close() // close connection
body, err := io.ReadAll(resp.Body)

// POST request
resp, err := http.Post(url, content_type, &buf)
defer resp.Body.Close() // close connection
body, err := io.ReadAll(resp.Body)

sort

import "sort"

// sort strings
sort.Strings([]string{"x", "y"})

// sort ints
sort.Ints([]int{5, 4})

// sort float64s
sort.float64s([]float64{1.4, 1.9})

// sort other types
arr := []int64{5,6}
sort.Slice(arr, func(i, j int) bool {
    return arr[i] > arr[j]
})

slices

import "slices"

// biggest number
slices.Max(slice)

// smallest number
slices.Min(slice)

// clone slice
slices.Clone(slice)

// reverse slice (modifi slice)
slices.Reverse(slice)

// sort slice (modifi slice)
slices.Sort(slice)

// index of element, -1 if don't
slices.Index(slice, value)

// if 2 slices are same
slices.Index(slice, slice2)

time

Delay

import "time"

// ms
func delayMs(ms time.Duration) {
    time.Sleep(ms * time.Milisecond)
}

// sec
func delaySec(s time.Duration) {
    time.Sleep(s * time.Second)
}

math/rand

Random number

import (
    "time"
    "math/rand"
)

func randInt(maxNumber int) int {
    // reset time, so it will be random
    newTime := rand.NewSource(time.Now().UnixNano())
    resetRandom := rand.New(newTime)
    
    // get random number
    randomNumber := resetRandom.Intn(maxNumber)
    
    return randomNumber
}

testing

methods

import "testing"

func TestFunction(t *testing.T) {
    // throw error and stop
    t.Error("message")

    // throw error and contiune
    t.Fail("message")

    // print message
    t.Log("message")
    t.Logf("message %d", 1)
}

reflect

type-of

import "reflect"

// get type of variable
reflect.TypeOf(<variable>) // return type

Todo

  • stdlib -> encoding, strconv, strings, bytes, crypto, os, maps

Project ideas

About

My GoLang projects with cheatsheet

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages