Coding Journal

My journey to become a Full Stack Developer

cover image

Learning Golang - day 1

May 21, 2022, 7:25 p.m.

I just decided to start learning Go again. I was learning it a bit last autumn, but then I had a very rough time in my personal life and also decided to start looking for a frontend job with vanilla JavaScript. I got a job where I can learn on the job anything I need for it. So in my free time, after work, I want to focus on something a bit different. 

My plan is to learn a bit each day and preferably take some notes. I have quite a lot of materials - books, and online courses. I also read a lot good about Go documentation and tutorial in the docs. In order not to struggle with analysis paralysis, I want to use one resource at a time starting from Head First Go. 

Things I learned today:

1. Main structure of a file in Go:

package main
// the name of the package -  we call the function the same

import (
	"fmt"
	"reflect"
)

// Here we import other packages we are going to use in our code.


func main() {
	fmt.Println("Hello, Go!")
	fmt.Println(reflect.TypeOf("Hello, Go!"))
}

// Then goes the function with its body between moustache brackets.

2. Go is a strictly typed language. 

It means that when we are creating a variable we should declare its type (string, boolean, int, float64 and so on) and while we can change the value of the variable, we can't change its type later on in the program. 

3. There are at least 3 ways of declaring variables: 

 - we can declare a variable without assigning any value to it

var name string;
var age int;
var price float64;

This way, the value of each variable is 0 (for strings 0 means an empty string, for boolean it's false)

 - when we declare a variable and assign a value, we don't have to declare the type because Go recognizes the type from the value

var city = "Warsaw"
var length = 3.5

Now, it knows that city is a string and length is a float64

 - while declaring the variable with its value, we can use a shorter declaration form

quantity := 4
fruit := "apple"

4. We can convert types. 

If we want to make mathematical calculations we can't add an integer to a float number in Go. We need to convert an integer into a float number. We can also convert a float number into an integer but it can change its value. 

allApples := 3
eatenApples := 0.5
applesLeft := float64(allApples) - eatenApples
fmt.Println(applesLeft)

5. If we declare a variable, we need to use it.

Otherwise, we will have an error. The same imported but unused packages. 

6. Variables and functions usually start with a lowercase. 

But if we want to make it possible to use our package in another program and make some of the variables available there, we need to start them from an uppercase.

So far I've been using "fmt" package for printing the output of my code and it was possible thanks to the Println method. 

 

It's been mostly the repetition of what I learned in the autumn. But I think it's a good start. 

Tagged in : go/golang

my photo

written by

Makneta

Avid learner, Python / Django and CSS Art ethusiast.

Similar posts

All posts