100daysofcode - Day-02
Hello folks, Here we go, new day Day-02 and some new knowledge gained in Golang. Today we will dive in the GoLang data types.
GoLang and the different data types
-
Go is a statically typed language. Which means that variable types are known at compile time
-
Go use type inference where the compiler can infer the type of a certain variable without explicitly specify it by the developers
-
Primitive data types in GoLang
-
Signed Integer Types
(int8, int16, int, int32, int64) -
Unsigned Integer Types (
uint8, byte, uint16, uint, uint32, uint64, uintptr) -
Others (
bool, float32, float64, complex64, complex128)
Rune and Strings in Golang
-
What is a Rune ?
- Code points define characters in unicode. In the case of Go there is a new term called rune.
- Runes are aliases of type
int32 - To find a char rune we can simply write the following piece of code:
-
str:= 'B'
fmt.Println(rune(str))
// Output: 66
-
What is a String ?
-
String is a data type that represent a slice of bytes that are UTF-8 encoded. Strings are enclosed with double quotes.
strExample := "Hello everyone, this a string" -
We can get the String length by typing the following
len([]rune(strExample)), or len(strExample) -
We can use the
rune()function to convert string to an array of runes -
Moving from String to rune
-
strExample := "Hello GoLang"
fmt.Println([]rune(strExample))
Output: [72 101 108 108 111 32 71 111 76 97 110 103]
Type Aliases
- Type aliasing refers to the technique of providing an alternate name for an existing type, with an aim to facilitate code refactor for large codebases.
Example:
-type productID int
-type productPrice float64
