Golang data types, variable and Constants¶
Golang has the following data types
- Basic type:
- Numbers
- Integers
- Floating-Point Numbers
- Complex Numbers
- Strings
- Booleans
- Numbers
- Aggregate type:
- Array
- Structs
- Reference type:
- Pointers
- Slices
- Maps
- Functions
- Channels
- Interface type
Numbers¶
In go language we have signed and un-signed integers. Let's see the table of integers that we can define to save the memory
Data Type | Description |
---|---|
int8 | 8-bit signed integer |
int16 | 16-bit signed integer |
int32 | 32-bit signed integer |
int64 | 64-bit signed integer |
uint8 | 8-bit unsigned integer |
uint16 | 16-bit unsigned integer |
uint32 | 32-bit unsigned integer |
uint64 | 64-bit unsigned integer |
int | Both in and uint contain same size, either 32 or 64 bit. |
uint | Both in and uint contain same size, either 32 or 64 bit. |
rune | It is a synonym of int32 and also represent Unicode code points. |
byte | It is a synonym of int8 . |
uintptr | It is an unsigned integer type. Its width is not defined, but its can hold all the bits of a pointer value. |
Example:
package main
import "fmt"
var myInt8 int8 = 2
func main(){
var myInt int = 2506
fmt.Println(myInt8, myInt)
}
Like above we can define the integer variables. Choose the integer type based on the number size.
Strings¶
String
is immutable slice of bytes and the bytes of the strings can be represented in utf-8 encoding.
Example:
package main
import "fmt"
var myStr1 string = "Hello Wolrd!!"
func main(){
myStr2 := "This is Batta"
fmt.Println(myStr1, myStr2)
}
Booleans¶
Booleans has only two values true
or false
. It's data type is bool
Example:
package main
import "fmt"
var valid bool = true
func main(){
invalid := false
fmt.Println(valid, invalid)
}
Array¶
In Golang, array is a fixed-size collection of elements of the same type. The elements of the array are stored sequentially
Example:
package main
import "fmt"
func main() {
var a [2]string
a[0] = "Hello"
a[1] = "World"
fmt.Println(a[0], a[1])
fmt.Println(a)
primes := [6]int{2, 3, 5, 7, 11, 13}
fmt.Println(primes)
}
Structs¶
A struct
is a collection of data fields with declared data types. Golang has the ability to declare and create own data types by combining one or more types, including both built-in and user-defined types.
Example:
package main
import "fmt"
type rectangle struct {
length float64
breadth float64
color string
}
func main() {
fmt.Println(rectangle{10.5, 25.10, "red"})
}
Pointers¶
A pointer is a variable that stores the memory address data referenced by another variable. Pointers have the power to mutate data they are pointing to
Example:
package main
import "fmt"
func main() {
var title string = "Google"
var pointer *string = &title
fmt.Println("title =", title)
fmt.Println("pointer =", pointer)
fmt.Println("pointer data =", *pointer)
// lets change the data
*pointer = "Bing"
fmt.Println("title =", title)
fmt.Println("pointer =", pointer)
fmt.Println("pointer data =", *pointer)
}
Slices¶
A Slice is a segment of an array. Slices build on arrays and provide more power, flexibility, and convenience compared to arrays.
Example:
package main
import "fmt"
func main() {
var a = [5]string{"Alpha", "Beta", "Gamma", "Delta", "Epsilon"}
// Creating a slice from the array
var s []string = a[1:4]
fmt.Println("Array a = ", a)
fmt.Println("Slice s = ", s)
}
Maps¶
A map
is an unordered collection of key-value pairs. It maps keys to values. The keys are unique within a map
.
Example:
package main
import "fmt"
func main() {
var m = map[string]int{
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5, // Comma is necessary
}
fmt.Println(m)
fmt.Println(m["one"])
}
Functions¶
A function is an independent section of code that maps zero or more input parameters to zero or more output parameters.
Example:
package main
import "fmt"
// function
func average(xs []float64) float64 {
total := 0.0
for _, v := range xs {
total += v
}
return total / float64(len(xs))
}
func main() {
xs := []float64{98,93,77,82,83}
fmt.Println(average(xs))
}
Channels¶
Channels can be thought as pipes using which Goroutines
communicate. Similar to how water flows from one end to another in a pipe, data can be sent from one end and received from the another end using channels.
Example:
package main
import (
"fmt"
"time"
)
func hello(done chan bool) {
fmt.Println("hello go routine is going to sleep")
time.Sleep(4 * time.Second)
fmt.Println("hello go routine awake and going to write to done")
done <- true
}
func main() {
done := make(chan bool)
fmt.Println("Main going to call hello go goroutine")
go hello(done)
<-done
fmt.Println("Main received data")
}
Defining Variables¶
A variable is a named unit of data that may be assigned a value.
Declaring variables using var
¶
Example:
package main
import "fmt"
func main(){
var ten int = 10;
var twenty int;
twenty = 20
fmt.Println(ten)
fmt.Println(twenty)
}
Short form of declaring variables without var
¶
Example:
package main
import "fmt"
func main(){
ten := 10;
fmt.Println(ten)
}
Interface type¶
Interfaces in Go do not enforce a type to implement methods but interfaces are very powerful tools. A type can choose to implement methods of an interface. Using interfaces, a value can be represented in multiple types, AKA, polymorphism.
Example:
package main
import "fmt"
type Rock struct {
Mass int
Volume int
}
func (r Rock) Density() int {
return r.Mass / r.Volume
}
func IsItDenser(a, b Dense) bool {
return a.Density() > b.Density()
}
type Geode struct {
}
func (g Geode) Density() int {
return 100
}
type Dense interface {
Density() int
}
func main() {
r := Rock{10, 1}
g := Geode{}
// Returns true because Geode's Density method always
// returns 100
fmt.Println(IsItDenser(g, r))
}
Go Constants¶
Go supports constants of character, string, boolean, and numeric values. const
declares a constant value.
package main
import (
"fmt"
"math"
)
const s string = "constant"
func main() {
fmt.Println(s)
const n = 500000000
const d = 3e20 / n
fmt.Println(d)
fmt.Println(int64(d))
fmt.Println(math.Sin(n))
}
Output:
constant
6e+11
600000000000
-0.28470407323754404