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.
Top comments (23)
Nice! 👏 I have also started to look at Go, although very lightly and just for fun. I like static types a lot.
The compile times in Go are very good, and the Tour of Go is a nice starting point, although I wouldn't accumulate resources, because this usually lead to overload and frustration. Like you perfectly put yourself, one resource at a time.
I think my two biggest beginner struggles were knowing how to iterate over a string and how to initialize a map without keys to add them later. 😅
I have been learning lightly for a few months...I'm still a little unclear on the value of an array variable when its not initialized, such as
var arr [3]int
.I'm also interested in learning Go, but It's been for a while since I studied because of my work. Thanks for sharing the post :) I'm looking forward to the next article.
Just started my first dev job so I know what you mean by being busy b/c of work. And I think that on weekdays I will have much less time for learning and writing but want to keep consistent even if it's a half an hour a day.
That's a good saying. Even if it's a half an hour a day,
I'll start it from now on.
Thank you for commenting.
I wish luck for your first job :) good coding!
Great article nice to see you writing more now 😁 I hear that Go is very similar to Python I was debating learning Go one day.
I'm not sure how much Go is similar to Python, I know it too little yet.
Quite similar I believe. I used to know another developer she knew Python and Go and learned them at the same time. She sometimes accidentally wrote Python in Go and vice versa because of the similarities 😂
For me Python and Javascript are quite similar (not including the dom manipulation). In the beginning I was writing Javascript as if it was Python with curly brackets.
In Go, the thing that's quite different from Python are types. And that you can't write anything without putting it into a function. At least those are my observations so far.
Thank you.
I'm going slowly through my textbook ;-).
Naming the packages on the top of the file confuses me a bit. But hope to find out how it works in not "main packages", soon.
Congratulations!
Thank you.
You did a very good job, only one notice: converting variable from a type to another is called "casting" :)
Thank you.
I'm using a book in Polish to learn Go and then I'm taking notes in English ;-) so sometimes I'm not checking how things are called properly, my mistake.
No mistake, just a step forward in the learning process :)
Sure, there is no learning without mistakes.
I guess my confusion is when is an array, slice, or map variable nil, if ever.
Very good Magda, I'm learning Go too, so I hope to see your next steps in this language
Hope you keep writing, I'll be using your notes to learn go too, thank you so much ❤