Golang
Go (or Golang) is a statically typed, compiled programming language designed at Google.
Strings
A String in Go is a slice of bytes.
Pointers
When you need to reference, i.e., get the memory address of a variable, use an & in front of it.
When you need to dereference a pointer to access its value, use an * in front of it.
package main
import (
"fmt"
"log"
)
func main() {
var userName string = "John Smith"
fmt.Println("Your name is :", userName)
log.Println("BEFORE 'changeUsingPointer' func call 'userName' is set to : ", userName)
// Logging the pointer for the 'userName' variable
log.Println(&userName)
changeValueUsingPointer(&userName, "Gregory")
log.Println("AFTER 'changeUsingPointer' func call 'userName' is set to : ", userName)
}
func changeValueUsingPointer(oldValuePointer *string, newValue string) {
log.Println("'oldValuePointer' is set to : ", oldValuePointer)
*oldValuePointer = newValue
}
Structs
Structs are typed collections of fields, useful for grouping data together to form records.
type User struct {
FirstName string
LastName string
PhoneNumber string
Age int
BirthDate time.Time
}
Adding a Function to a Struct Using a Receiver
*User refers to the struct created for the user.
Maps
Maps are Go's built-in associative data type (sometimes called hashes or dicts in other languages).
technologiesUsed := make(map[string]string)
technologiesUsed["database"] = "Postgres"
technologiesUsed["frontend"] = "HTMX"
log.Println("Using:", technologiesUsed["database"], "and", technologiesUsed["frontend"])
- You can store whatever you want in a map, even custom types.
- Maps are very fast and mutable.
- Maps are not sorted by default; do not depend on their order.
Slices
In Go, you typically use Slices instead of plain arrays. Slices are dynamically-sized, flexible views into the elements of an array.
var myStrings []string
myStrings = append(myStrings, "Golang")
myStrings = append(myStrings, "Javascript")
log.Println(myStrings) // [Golang Javascript]
numbers := []int{4, 56, 76, 20, 43, 11}
log.Println(numbers) // [4 56 76 20 43 11]
sort.Ints(numbers)
log.Println(numbers) // [4 11 20 43 56 76]
Slices, like arrays, are zero-indexed.
Getting a Range of a Slice
Control Structures
If Statements
func (u *User) canVote() bool {
var result bool
if u.Age > 18 {
result = true
} else {
result = false
}
return result
}
Loops
Go has only one looping construct: the for loop.
for i := 0; i < len(myStrings); i++ {
fmt.Println(myStrings[i])
}
// Using range
for _, num := range numbers {
fmt.Println(num)
}
for i, num := range numbers {
fmt.Println(i, num)
}
Interfaces
Interfaces are named collections of method signatures.
package main
import "fmt"
type Animal interface {
Says() string
NumberOfLegs() int
}
type Dog struct {
Name string
Breed string
}
func (d Dog) Says() string {
return "Woof"
}
func (d Dog) NumberOfLegs() int {
return 4
}
func main() {
dog := Dog{
Name: "Sam",
Breed: "Bulldog",
}
PrintInfo(dog)
}
func PrintInfo(a Animal) {
fmt.Println("This animal says", a.Says(), "and has", a.NumberOfLegs(), "legs")
}