Get your instance HERE
Join the support channel HERE
Find more from the author @ akondas.com
:q!
- quit without saving:wq
- save and quit:w
- just save:q
- quit if saved
For the sake of our playground, we've enabled arrow keys in our Vim instances. If you want to be a pro:
h
- leftj
- downk
- upl
- right
v
- visual mode, lets you highlight stuffi
- insert mode, lets you input stuffesc
- magic key, switches to normal mode where you can move around
Bear in mind, this cheat sheet is really short. If you want to learn more, check A Tour of Go.
Go is statically typed language - meaning that each variable has to have a pre-defined type; either by the user or by right-hand value/return value.
var msg string
as:
var
- keyword for initializing variablemsg
- name of the variablestring
- type of the variable
msg = "Hello"
- assign value to pre-initialized variable
shorthand:
msg := "Hello"
- initialize variable, assing the value. This way variable inherits type of the value
str := "Hello"
str := `Multiline
string`
// int
num := 3 // int
// float
num := 3. // float 64
In Go arrays have a fixed size
numbers := [...]int{0, 1, 2, 3, 4}
[...]
means that the size will be set by values in ( )
.
Think about them as dynamic arrays.
slice := []int{1, 2, 3}
This is a very VERY long topic but let's put it this way - pointers are the reference to a memory address of a variable.
var p *int
- this means that p is a type of pointer value to int&
- generates a pointer to it's operand. You can use it in shorthand variable assignment.*
- denotes(references) a value of the pointer
interface{}
this is a magical type that will inherit any type. Very useful in JSON.
You can use one of two, both have the same methods:
log.Print(value) // prints nicely logged values
fmt.Print(value) // prints straight to stdout
Define function as:
// second () is a return type
// you can do named returns although it's not the best practice
func FunctionName(parameter string) (string) {
}
So, here's a thing - if you come from Java, JavaScript or even Python: Go doesn't support OOP. In a normal way.
In Go we don't create classes, we create structs that will have methods.
// this will create a struct and define the internal variables and their types
type Example struct {
// if the struct field is capitalised
// it will be exported
// meaning - will be available outside of the struct
Number int
Field string
// nested field - think about this as a type inheritance
// composition in Go is another topic
Nested Nested
// if the struct field is lowercased
// it won't be exported
// meaning - it won't be accessible outside of the struct
internals string
}
// Initialize type
t := Example{}
log.Print(t.internals) // not accessible, won't even compile
log.Print(t.Number) // will print the number
// this function is a method of Example type
// note (e *Example) - that means that this function references the type
// so we'd be able to use fields that are not exported
func(e *Example) WillPrintUnexportedField() {
log.Print(e.internals)
}
t.WillPrintUnexportedField() // prints to stdout whatever the internals field value is
go run .
- compiles and runs the app.
go build .
- compiles the app in the current dir and generates an executable.