Learning GoLang - Day 1 : 初见
Actually, this may not be the first time I started to learn Golang, but I didn't take it seriously. Currently, because of my working needs, I have decided to study it every weekday and one hour each day. After the learning hour is up, I'll share some important notes here.
Today, following the A Tour of Go, I learned the language basics. Its concepts are not too hard to understand, especially if you know the C language well before. But there are several points that I think are pretty important to highlight which show the part of the uniqueness of this language.
- Simpler and varied syntax of expressions. You can feel this from the flow control statements, e.g. flexible for loops, defer statements, etc.
- The power of Pointers is not as strong as those in C. There is no pointer arithmetic for Golang. It also indicates the nature of the pass-by-value for Golang. On the other hand, it enhances the security of its usage. You can also notice Go decides to make it more convenient for usage by interpreting automatically between value and its pointer(e.g. when getting elements or calling methods with
pointerreceivers). - Interesting but dangerous slices. Until now, the most complex concept that I have found is the slice. They are like references to arrays, so they don't store data. You might be confused by the size of its length and capacity, especially when you try to get a slice from another slice. There is one key that, there are huge differences depending on if you drop the first element of the original slice.
s := []int{2, 3, 5, 7, 11, 13}
// Slice the slice to give it zero length.
s = s[:0]
// len=0, cap=6
// Extend its length.
s = s[:4]
// len=4, cap=6
// Drop its first two values.
s = s[2:]
// len=2, cap=4
This is what I learned today, see you tomorrow.