Go Crash Course (Part 2)
We are going to cover syntax in this section. The syntax is an essential part of programming languages. Some are similar to others. Some are distinctive into themselves. You can go to the previous section where there is the installation instruction if Go is not yet installed on your system.
Variables
|
|
The var
statement declares a list of variables. It can include initializers, one per variable. Inside a function, the :=
short assignment statement can be used in place of a var
declaration with implicit type.
Constants are declared like variables, but with the const
keyword, and cannot be declared using the :=
syntax.
As we can see, number and text are global variables because they are defined outside of a function and on the top of the program. They can be accessed inside any of the functions defined for the program.
The boolean local
variable is a local variable inside the main
function. It can be only accessed inside the main
function.
We printed the text
variable in the printText
function. The variable has the same name as the global one. But the one that we printed is an argument variable. It was overwritten.
Types
|
|
Here is the output.
false Lorem ipsum. -9 9 824634117815 97 256 3.1415927 3.1415927410125732 (1+1i) (2+1i)
We didn't initialize the value for the global
variable. The default or zero value for boolean is false, 0 for numerics, and "" (the empty string) for strings.
The difference between int
(signed integer) and uint
(unsigned integer) is it can't handle negative value and has a larger maximum value for unsigned one.
The int
, uint
, and uintptr
types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems. The number appended after the type such as int8
, int16
, and so on represents bits or size. 8 bits is equal to 1 byte.
The uintptr
type is to store the address in number. The value is the pointer address but in an integer format.
Take a look at the pi
variable. We can't store or print the actual value if the size is not enough. Then we cast float32
to float64
for fullPi
to store the actual value.
The complex (complex64
or complex128
) data type is to store a real and imaginary number (i
symbol) of float. float32
for complex64
and float64
for complex128
.
It will cast the data type automatically for shorthand. Refer to the inference
variable.
The byte
type is an alias for uint8
. The rune
type is an alias for int32
. The byte is to store ASCII characters in number (0-255). The rune is to store UNICODE characters in number (-231 to 231 -1).
Pointer
|
|
The pointer is to store the address of a variable. The &
is used to get the address of the variable. The *
is used to present the value from the address of the pointed variable.
We can see that we did increment on the *pointer
variable and it applied to the number
variable.
Struct
|
|
A struct
is a collection of fields.
Array
|
|
Array in Go and how we sliced the array to get the value.
Map
|
|
Map, key-value pairs, in Go.
For
|
|
Go only used the for
syntax for looping. Take a look at the code above about how we implement the while
loop with for
in Go.
If
|
|
The for
and if
have no braces in Go.
Switch
|
|
The break
is optional. The switch
can contain no condition. It could be a clean way to write long if-then-else chains.
Defer
|
|
A defer
statement defers the execution of a function until the surrounding function returns.
Conclusion
The reference for this article is from the official website of Go. You can practice more at https://tour.golang.org/list. You can get the source code at https://github.com/aristorinjuang/go-crash-course_02.
Related Articles
- Go Crash Course (Part 1)
- Simple REST API with Go
- Intel Edge AI Scholarship Foundation Course, OpenVINO Fundamentals
- Microservices
- Python for Data Science and Machine Learning Bootcamp