19. User Defined Types

User Defined Type

We can defined our own type based on existing Go in-build types. We have declared person as user defined type based on struct. We are not limited to struct. We can define our type based on any other type as base type.

Declaring our own type

We can declare our type using type keyword. It generally takes form of
type type_name base_type
main.go
1
type marks int
2
type liters float64
3
4
var m1 marks
5
m1 = 20
6
7
var l1 liters
8
l1 = 3.4
Copied!
marks and liters are types based on int and float64 respectively.
Go defined types most often use structs as their underlying types, but they can also be based on ints, strings, booleans, or any other type. - Head First Go
Last modified 2yr ago