Golang
Go or Golang is a programming language that designed at Google.
The language can be described by these following items:
- it’s compiled language
- similar to C language by syntax
- the language has:
- Official website
cli
- **go run <module> - run the file/module as a program
- go test - run tests
- go mod init <module> - initialise a module
godoc
It provides an ability to open Go documentation locally and even offline
Install
|
|
Run
|
|
“Hello, World!” Example
|
|
- Go has standard library that is built-in modules of functions
- for example: fmt is a built-in package
- Println(value) is a method that prints value in a console/terminal and also prints break line in the end
- func main() is a function
- it is a main function of the module and it will be executed automatically when you run command go run hello.go
Variables
Declaring
Example 1:
|
|
Example 2:
|
|
Example 3 (with type inference):
|
|
- type inference is when you declare variable without specifying a type
- the variables type is inferred by the value
Types: string
Declaring a string variable
Example 1:
|
|
Example 2:
|
|
Example 3 (with type inference):
|
|
concatenating
An example:
|
|
add a number to a string
JavaScript devs be like:
|
|
Types: int
Declaring an int (number) variable
Example 1:
|
|
Example 2:
|
|
Example 3 (with type inference)
|
|
Interesting stuff
if I declare variable like this:
|
|
it will print the result: 0
Operations: add (+)
|
|
Reassigning a variable
|
|
Operations: multiply (*)
|
|
Types: float64
An example:
|
|
- anotherOneLuckyNumber will still be integer (int), no numbers after decimal point
- we should use float64 type to have full number with part after point
Using float64
We need to declare that variable anotherOneLuckyNumber should be with type float not int:
|
|
- there will be an error
- we need to convert luckyNumber into float64 but only in operation of dividing with 8 - for this we can use a command (function) float64
|
|
Types: float32
The difference between types float32 and float64 is how many numbers will you have after the point sign.
For example, the result of operation will be:
- 17 / 7 =
- float32
- 2.4285715
- float64
- 2.4285714285714284
- float32
An example with float32:
|
|
An example with float64:
|
|
Types: rune
single unicode character
An example:
notice that there is a single quotes ('’) instead of double quotes ("") because double quotes are used for string type
|
|
- 128077 is a special identifier for the sign ‘π’
to print the sign π as it is instead of integer we need to convert it to a string by function string()
|
|
Types: byte
single byte (ASCII character)
An example:
notice that there is a single quotes ('’) instead of double quotes ("") because double quotes are used for string type
|
|
To print a ‘c’ value as it is we need to convert value to a string by using function string()
|
|
Types: byte vs rune
A type byte is a smallest type in Go so you can initialise a variable only with one character (letter or number) but not with something like emoji or special character/symbol like ‘β¬’
|
|
so you need to use type rune for such characters/symbols
Formatting strings (fmt)
- (package) fmt
- print the argument str in command line (terminal)
- .Print(str) - prints *str as it is
- .Printf(formatStr, args…)
- .Println(str) - as .Print(str) but adds “\n” in the end of string
- return a new formatted string
- .Sprint(str) - returns a new formatted string, uses default formats
- .Sprintf(formatStr, args…)
- .Sprintln(str) - as .Sprint(str) but adds “\n” in the end of string
- write a new formatted string to specified writer (for example: a file or command-line/terminal)
- .Fprint(writer, str) - writes a new formatted string to a writer (file, command-line, etc)
- .Fprintf(writer, formatStr, args…)
- .Fprintln(writer, str) - as .Fprint(str) but adds “\n” in the end of string
- print the argument str in command line (terminal)
Examples
Printf(formatStr, args…)
|
|
Sprintf(formatStr, args…)
|
|
Fprintf(writer, formatStr, args…)
|
|
Modules vs Packages
- Go code is organised in modules and packages
- modules
- are bigger than packages
- has unique identifier (name)
- can be distributed (as a library)
- every Go project is a module
- projects can use multiple modules
- created and managed by commands go mod <smth> and file go.mod
- packages
- modules contains packages (at least one package - for example main)
- package can have multiple files
- packages are stored in subfolders in a module
- can be imported
- modules
Pascal Case vs Camel Case naming of variables
- pascal case
var Greeting string = "Hello there!"
- in Go it means that variable will be used also outside of the package where was declared
- camel case
var greeting string = "Hello there!"
- in Go it means that variable will be used only inside the same package as where it was declared
An example:
camel case
variables.go
|
|
hello.go
|
|
pascal case
variables.go
|
|
hello.go
|
|
Constants
Constant is like a variable but it cannot be reassigned. Constant will have the same value.
|
|
Writing tests in Go
- File should have name like “xxx_test.go”
- The test function must start with word “Test”
|
|
- The test function has one argument “t *testing.T”
- Need to import “testing” to use argument “t *testing.T”
t.Errorf()
- t.Errorf() - prints message fail the test
|
|
t.Run()
it seems like an equivalent of “describe” or “it” in [[JestJS]]
|
|
t.Helper()
- it declares that the method where it was called is a helper function
- because of this the line number of the assertion (where helper function is called) will be printed out
|
|