Skip to content

marcin-janas/hello-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hello Go

My place to collect information about Go

Main function

package main

func main() {
	println("Hello, Go!") // Built-in println function
}

Keywords

Go only has 25 keywords!

    break, case, chan, const, continue,
    default, defer, else, fallthrough, for,
    func, go, goto, if, import,
    interface, map, package, range, return,
    select, struct, switch, type, var

"Syntax is clean and light on keywords"

Built-in functions

There is no guarantee that the print and println functions will stay in the language!

    append, cap, close, complex, copy,
    delete, imag, len, make, new,
    panic, print, println, real, recover

Basic types

string
bool
uint, uint8, uint16, uint32, uint64, uintptr, int, int8, int16, int32, int64
byte // alias for uint8
rune // alias for int32
float32, float64
complex64, complex128
pointer, function, interface, slice, channel, map
array, struct

Zero values

"" // for strings
false // for booleans
0 // for numeric types
nil // for pointers, functions, interfaces, slices, channels, and maps
// each element of an array of structs will have its fields zeroed if no value is specified

Names

  1. "Avoid package names like base, common, or util"
  2. "A package's name should describe its purpose"
  3. "A variable's name should describe its content"
  4. "Use the smallest scope possible, declare variable close to their use"
  5. "Functions should be named for the result they return"
  6. "Methods should be named for the action they perform"
  7. "Methods mutate state, functions transform data"
  8. "The visibility of a name outside a package is determined by whether its first character is upper case"
  9. "Finally, the convention in Go is to use MixedCaps or mixedCaps rather than underscores to write multiword names"

Constants

package main

const (
	STR = "string"
	_   = iota // ignore first value
	TWO
	SIX = iota * TWO
	EIGHT
	TEN
)

func main() {
	println(STR, TWO, SIX, EIGHT, TEN)
}

Variables

package main

var (
	str1 = "test"
	int1 int
)

func main() {
	var str2 string
	int2 := 2
	println(str1, int1, str2, int2)
}

New and make

The new built-in function allocates memory. The first argument is a type,
not a value, and the value returned is a pointer to a newly allocated zero
value of that type.
package main

func main() {
	p := new(int) // pointer to int
	println(p)
	println(*p)
}
The make built-in function allocates and initializes an object of type
slice, map, or chan (only). Like new, the first argument is a type, not
a value. Unlike new, make's return type is the same as the type of its
argument, not a pointer to it.
package main

func main() {
	s := make([]int, 10) // slice with len(s) == cap(s) == 10
	println(s)
	println(s[0])
}

Packages

Import

package main

import (
	"fmt"
	"log"
)

func main() {
	fmt.Println("Hello, Go!") // Println function from the fmt package
	log.Println("Hello, Go!") // Println function from the log package
}

Init

package main

func init() {
	println("init")
}

func main() {
	println("main")
}

Blank identifier

package main

import _ "fmt"

func main() {
	str := "test"
	for _, r := range str {
		_ = r
	}
}

Unused

package main

import "fmt"

func main() {
	msg := "Hello, Go!"
}

If

"A language needs only one conditional control flow construct"

package main

func main() {
	a := 0
	if a > 0 {
		println(">")
	} else if a < 0 {
		println("<")
	} else {
		println("=")
	}
}

For

"Go has only one looping construct, the for loop"

package main

func main() {
	for {
	}

	s := []int{1, 2, 3, 4, 5}
	for n, v := range s {
		println(n, v)
	}

	for i := 0; i <= 9; i++ {
		println(i)
	}
}

Switch

"A switch statement is a shorter way to write a sequence of if - else statements"

package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	rand.Seed(time.Now().UnixNano())
	r := rand.Intn(3)
	switch r {
	case 0:
		fmt.Println(0)
	case 1:
		fmt.Println(1)
	default:
		fmt.Println(2)
	}
}

Function

package main

func f() {}

func echo(s string) {
	println("echo: " + s)
}

func sum(i, j int) int {
	return i + j
}

func swap(i, j int) (int, int) {
	return j, i
}

func main() {
	f()
	println(f)

	echo("func")

	println(sum(1, 2))
	println(swap(1, 0))
}

Struct

package main

import "fmt"

type rectangle struct {
	description string
	a, b        int
}

func main() {
	r := rectangle{"square", 2, 2}
	fmt.Println(r)
}

Methods

package main

import "fmt"

type rectangle struct {
	description string
	a, b        int
}

func (r rectangle) area() int {
	return r.a * r.b
}

func main() {
	r := rectangle{"square", 2, 2}
	fmt.Println(r.area())
}

Testing

package main

import (
	"testing"
)

func TestArea(t *testing.T) {
	tests := []struct {
		name string
		rect rectangle
		want int
	}{
		{
			"empty rectangle",
			rectangle{"empty", 0, 1},
			0,
		},
		{
			"special rectangle",
			rectangle{"square", 2, 2},
			4,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := tt.rect.area(); got != tt.want {
				t.Errorf("%s: want[%v] != got[%v]", tt.name, tt.want, got)
			}
		})
	}
}

"Files whose names begin with _ (including _test.go) or . are ignored."

Defer

package main

func main() {
	println("func main")
	f1()
}

func f1() {
	defer f2()
	println("func f1")
}

func f2() {
	println("func f2")
}

Error handling

package main

import "os"

func main() {
	if _, err := os.Open(""); err != nil {
		os.Exit(1)
	}
}

Panic and recover

package main

func main() {
	defer func() {
		if msg, ok := recover().(string); ok {
			print("recover: " + msg)
		}
	}()

	panic("panic msg")
}

Modules

go mod init
go mod tidy

Channels

package main

func main() {
	ch := make(chan string)
	go func(chan string) {
		ch <- "Hello, Go!" // write to channel
	}(ch)
	println(<-ch) // read from channel
}

Generics

package main

func add[T int | string](s []T) T {
	var r T

	for _, v := range s {
		r += v
	}

	return r
}

func main() {
	i := []int{1, 2, 3}
	s := []string{"One", "Two", "Three"}
	println(add(i))
	println(add(s))
}

Useful links

About

My place to collect information about Go

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages