Resources:
- Zero to Mastery academy
- Google
- Go simplified by Solomon Eseme
- AI
- http://go.dev/tour
- https://www.udemy.com/course/learn-how-to-code/learn/
### Day 22/100 ⚡️
_As a Go developer, the guide by Solomon has been helpful. It explained Go concepts clearly. If you're interested in learning Go for backend development, check it out!_
_1st August, 2023_, 5:00pm WAT, I learnt about:
- Refactoring code to make it clear
- String Formatting and using Placeholders: `%s` for string and `%d` for integers
- `fmt.Sprintf` and `fmt.Println`
- In Go, you can take advantage of the `Type Inference` which allows the compiler to automatically infer the variable type based on the initial value assigned to it
`fmt.Sprintf` - _It is a function in Go that formats and returns a string based on a format string and a list of values. It creates a formatted string without immediately printing it to the console. It's like composing a message but not showing it yet._
`fmt.Println` - _It is also a function in Go that prints the provided values to the console as separate lines. It doesn't format the values but prints them as they are. It's like showing the composed message to the user._
In summary, `fmt.Sprintf` is used to format and create a string, while `fmt.Println` is used to print values or messages to the console.
### Day 23/100 ⚡️
_2/08_2023_ 5:31pm
- Go has an inbuilt Garbage collector.
- Structural typing via interface to build concurrent apps
I learnt about Variables in Go
- Declaring in blocks
```go!
var (
age = 2
yAge = 200
)
```
- Declaring and not assigning
```go=
var fName string
fName = "Ekay"
```
- Printing out usng the `fmt.PrintIn` function from the Go standard library.
```go!
fmt.PrintLn(fName)
```
_3/08/2023/_
### Day 24/100 - building a news app with Go
_how to add an image in markdown from mac?_
I read the Go Essentials book and documented what I learnt, find it [here.]()
### Day 25/100
- Created a draft for my Go article series
__Operators in Go__
- Operators
- Arithmetic
- Logical & Relational
```go=
// Arithmetic operators in Go
/* Addition + and +=
Subtraction - and -=
Multiolication * and *=
Division / and /=
Remainder % and %=
Increment and Decrement
i++
i--
*/
```
- Arithmetic operators always return a number
- Logic Operators - they ensure multiple criteria are met. They always return a boolean (true/false)
`&&` - And
`||` - Or
`!` - Not
Examples:
```go=
secured & transfered
grey || ash
!hard
```
- Relational Operators always return a boolean - True or false
`>` Greater than
`>=` Greater than or equal to
`<` Less than
`<=` Less than or equal to
`==` Equal to
`!=` Not Equal to
Recap
- Operators operate on operands
- Arithmetic operators enable calculations
- An arithmetica assignment can calculate and assign to a variable in a single statement.
- Relational operators enable comparisons
- Logic operators enable combination and negotiations.
A feature of Go - Statment Initialization
- Early Return
`if-else` `else-if` `else` logic operators
`:=` it means "create and assign", used to initialize a Variable in go
Switch Statement in Go
- Basic usage
- Case list - taking the same action for multiple different values
- Fall through(keyword) Behaviour - will continue checking the next case. (execute the next case, even if it doesn't match)
Switch is used to:
- easily check multiple conditions
- alternative to many `if-else-if` blocks.
- switches execute from top-to-bottom
- optionally have a default action
Switch can:
- easily check a variable for different values
- Use commas to check multiple values on a single case
- Expressions are allowed as a case
- Function calls, Math, Logic
- Got a scenario with lots of things to check? use Switch
Day 27 - 9/8/2023
### Loops :white_check_mark:
- one keyword for Looping in Go = `for`
`for: While`
- when looping, the condition must have a variable created. The initialization variable can be used only within the loop block.
- The post statement executes every iteration.
`for: Infinite`
- Infinite loops are used for servers, and reading data from a Data stream
- Use the `break` key word to break out the loop
`Continue` keyword to skip the current loop (iterations)
_Types of Loops_
1. the typical loop
```go=
for i := 0, i < 10; i++ {
// ....
}
```
2. the `while` loop (variable already created)
```go
for i < 10 {
// ...
}
```
3. the intentional infinite loop
```go
for {
// ...
}
```
_Solved the popular "FizzBuzz" problem using `for` loop._ It gets better all the way :sunglasses:
- Here's the code:
```go
package main
import "fmt"
func main() {
for i := 1; i <= 50; i++ {
// check for divisibility by 3 and 5
if i%3 == 0 && i%5 == 0 {
fmt.Println("FizzBuzz")
} else if i%3 == 0 {
fmt.Println("Fizz")
} else if i%5 == 0 {
fmt.Println("Buzz")
} else {
fmt.Println(i)
}
}
}
```
- [Go package registry](https://pkg.go.dev/std)
Created a Dice roller game. `Seeding` make sure the random numbers are actually random.
`math/rand` package - for random number generation
`time` package - for seeding the random number generator.
### Day 29
__Structures in Go__
- what they are?
- how they Define, Create and Access data.
- Structures in Go allow data to be stored in groups, similar to classes in other languages
### Day 30 ☑️
### Day 31
### Day 32
Arrays are a way to store multiple pieces of the same kind of data.
- Create
- Access
- Iterate
Each piece of data is called an __element__. To access items in an array, an __array index__ is used. Then index starts at __0__
Arrays are fixed sized, and can not be resized.
- Visualization of Arrays in memory
| index | Address |
| ---- | ------- |
| 0 | 0x0 |
| 1 | 0x7 |
| 2 | 0xE |
- Elements not addressed in array visualization will be set to default values.
```go
package main
import "fmt"
func main() {
var myArray [3] string
myArray[0] = ""
myArray[1] = "happy"
myArray[2] = ""
item1 := myArray[0]
fmt.Println(item1)
fmt.Println(len(myArray))
}
```
It's good practice to assign the element in a variable during iteration of arrays.
`len` function calculates how many values are in an array.
Attempting to access an element outside the bounds of an array will result in an error.
Errors:
- Run Time
- Compile Time
- Use the `len()` function to iterate arrays in a `for` loop.
### Day 32 - Coursera
A Go Workspace code base hase three subdirectories (Heirachy):
- src: contains source code files
- pkg: packages (libraries)
- bin: contains executables
Packages in Go
- Group of related source files
- Each package can be imported by other packages
- Enables software reuse
- First line of file names the package
- Clean seperation of code
- There always has to be one package called main - that's where execution starts.
- Building the main package generates an executabe program
- Main packege need a `main ()` function
- `main ()` is where code execution starts.
Go Tools
- `import` keyword is used to access other packages.
- Go standard library includes many packages, such as `fmt`.
Go Tool is a tool to manage Go source code, it got many commands.
- `go build` - compiles the program
- Arguments can be a list of packages or a list of `.go` files
- Creates an executable for the main package, same name as the first `.go` file.
- `.exe` suffix for Windows.
- `go doc` - prints documentation for a package
- `go fmt` - formats source code files
- `go get` - downloads and installs packages
- `go list` - lists installed packages
- `go run` - compiles `.go` files and runs the executable
- `go test` - runs test using files that end in `_test.go`
-------------
### Mastering Go with Micah
**April 2024**
http://go.dev/tour
- Day 1
Basics of the Go programming language:
- Packages, variable, and functions.
- Flow control statements: for, if, else, switch and defer
- More types: structs, slices, and maps.
- Methods and interfaces
- Generics - type parameters
- Concurrency, goroutines and channels - a core feature
a names is exported in Go if it begins with a capital letter. example `math.Pi`
example of a function that multiples two numbers:
```go=
package main
import "fmt"
func mult(y int, z int) int {
return y * z
}
func main() {
fmt.Println(mult(10000, 10000))
}
```