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
type marks int
type liters float64

var m1 marks
m1 = 20

var l1 liters
l1 = 3.4

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 updated