Go DataTypes
Links:
- 103 Golang Index
- Go - Basics
DataTypes¶
Basic Types¶
int8
,int16
,int32
,int64
uint8
,uint16
,uint32
,uint64
: used to represent unsigned (positive) integers.uint
is an alias foruint32
oruint64
based on platform.int
is an alias forint32
orint64
based on platform.float32
,float64
: zero before the decimal point separator can be omitted (-.5-3.-0. 1.4).- complex64, complex128.
byte
: alias for uint8rune
: alias for int32
Golang doesn't have a character type it uses byte and rune to distinguish characters from integers.
Tip
Double quotes should be used when working with strings
Go - Strings, Runes & Bytes¶
Composite Types¶
- Array has a fixed length, slice has a dynamic length.
var numbers = [4]int{1,2,3,4}
- Arrayvar numbers = []float32{1.1,2.2}
- Slice- We can also use walrus operator:
number := []string{"hi","ti"}
- Map is just like dictionary in Python
- Struct Type (User defined type)
- A struct is a sequence of named elements, called fields, each of which has a name and a type.
- A struct can be compared to class concept in Object Oriented Programming.
- Pointer Type
- A pointer is a variable that stores the memory address of another variable
- The value of an uninitialised pointer is
nil
var x := 45; ptr := &x
- Type of ptr is*int
Converting Types¶
- In the above example y is not modified but a new int value is returned - int to string is done usingSprintf
: x := fmt.Sprintf("%d",45)
- Unicode conversion : x := string(65)
- A
- Converting string to int/float/bool : var f1, err = strconv.ParseInt("45",32)
- second parameter is the precision.
- Atoi
(string to int) and Itoa
(int to string)
i, err := strconv.Atoi("-50")
s = strconv.Itoa(20)
// => i Type is int, i value is -50
fmt.Printf("i Type is %T, i value is %v\n", i, i)
// => s Type is string, s value is "20"
fmt.Printf("s Type is %T, s value is %q\n", s, s)
Defined Types¶
- A defined type also called a named type is a new type created by the programmer from another existing type which is called the underlying or source type
- A new defined type must have : new name and can have its new methods
- The underlying type provides the representation, operations and size of the newly defined type
- Even though the defined type and the source type share the same representation, operations and size they are different types. A new type it's not just an alias for an existing type, it's a completely new type.
- In the below example underlying type of speed is uint8.
- If we want to perform operations between source and defined types we must convert one type into the other type. A type can be converted to another type if they share the same underlying type.
- There is no type-hierarchy in Go
Why do we need defined types?
- We can attach methods to newly defined types
- Type safety: We must convert one type into another to perform operations with them.
- Readability: When we defined a new type let's say
type usd float64
we know that new type represents the US Dollar not only floats
package main
type usd float64
func main() {
var balance usd = 50.5
}
// usd is a named type and balance a variable of usd type
Aliases¶
- An alias declaration has the form:
type T1=T2
as opposed to a standard type definition which has the form:type T1 T2
- An alias declaration binds an identifier to the given type. It's the same type with a new name.
Types with different names are different types, but there is an exception to this rule and that is the aliased types.
byte
anduint8
are aliases or the same type with different names. The same is applicable torune
andint32
because rune is an alias for int32.- Aliases can be used together in operations without type conversions we've seen at defined types
You should use aliases with caution, they are not for everyday use!
Last updated: 2022-06-05