Following are the basic data types in Go:
Numeric // Integer Types uint8 // Unsigned 8-bit integers (0 to 255) uint16 // Unsigned 16-bit integers (0 to 65535) uint32 // Unsigned 32-bit integers (0 to 4294967295) uint64 // Unsigned 64-bit integers (0 to 18446744073709551615) int8 // Signed 8-bit integers (-128 to 127) int16 // Signed 16-bit integers (-32768 to 32767) int32 // Signed 32-bit integers (-2147483648 to 2147483647) int64 // Signed 64-bit integers (-9223372036854775808 to 9223372036854775807) // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // Floating Types float32 // IEEE-754 32-bit floating-point numbers float64 // IEEE-754 64-bit floating-point numbers complex64 // Complex numbers with float32 real and imaginary parts complex128 // Complex numbers with float64 real and imaginary parts // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // Other Numeric Types byte // same as uint8 rune // same as int32 uint // 32 or 64 bits int // same size as uint uintptr // an unsigned integer to store the uninterpreted bits of a pointer value Handling Overflows At compile time, a Go compiler can catch overflow errors.
[Read More]
Go - Data Type Intro
There are following data types in Go:
- Basic type: Numbers, strings, and booleans come under this category.
- Aggregate type: Array and structs come under this category.
- Reference type: Pointers, slices, maps, functions, and channels come under this * category.
- Interface type
Go - Mutability
In Go, only constants are immutable. However because arguments are passed by value, a function receiving a value argument and mutating it, won’t mutate the original value.
package main import "fmt" type Artist struct { Name, Genre string Songs int } func newRelease(a Artist) int { a.Songs++ return a.Songs } func main() { me := Artist{Name: "Matt", Genre: "Electro", Songs: 42} fmt.Printf("%s released their %dth song\n", me.Name, newRelease(me)) fmt.Printf("%s has a total of %d songs", me.
[Read More]
Go - Named and Unnamed Types
Types in Go are divided into 2 categories: named and unnamed. Besides predeclared types (such as int, rune, etc), you can also define named type yourself. E.g.:
type mySlice []int Unnamed types are defined by type literal. I.e., []int is an unnamed type.
According to Go spec, there is an underlying type of every type:
Each type T has an underlying type: If T is one of the predeclared boolean, numeric, or string types, or a type literal, the corresponding underlying type is T itself.
[Read More]
Go - String
In Go, string is an immutable array of bytes. So if created, we can’t change its value. E.g.:
package main func main() { s := "Hello" s[0] = 'h' } The compiler will complain:
cannot assign to s[0]
To modify the content of a string, you could convert it to a byte array. But in fact, you do not operate on the original string, just a copy:
package main import "fmt" func main() { s := "Hello" b := []byte(s) b[0] = 'h' fmt.
[Read More]
Go - Structs
Intro A struct is a collection of fields/properties. You can define new types as structs or interfaces. If you are coming from an object-oriented background, you can think of a struct to be a light class that supports composition but not inheritance.
Structs are blueprints — They are fixed and created at compile-time, but struct values fill them in runtime..
It’s like a class in OOP languages. Groups related data in a single type.
[Read More]
Go - Type conversion
The expression T(v) converts the value v to the type T. Some numeric conversions:
var i int = 42 var f float64 = float64(i) var u uint = uint(f) Or, put more simply:
i := 42 f := float64(i) u := uint(f) Go assignment between items of different type requires an explicit conversion which means that you manually need to convert types if you are passing a variable to a function expecting another type.
[Read More]
Go Pointers
Go has pointers, but no pointer arithmetic. Struct fields can be accessed through a struct pointer. The indirection through the pointer is transparent (you can directly call fields and methods on a pointer).
Note that by default Go passes arguments by value (copying the arguments), if you want to pass the arguments by reference, you need to pass pointers (or use a structure using reference values like slices (Section 4.2) and maps (Section 4.
[Read More]
Method with struct and interface
Methods enhance types with additional behavior.
Methods of the type are called Method Set.
To attach method to a type :
// Syntax // “varName Type” is called a “receiver” func (varName Type) funcName() { // Code }
// Example // “book” is a struct here func (b book) printBook() { fmt.Println(b.title, b.price) }
- A `receiver` is nothing but method's `input parameters` written `before` a `method name`. - A `method` belongs to a `single type`.
[Read More]