Code With Go
  • Code With Go
  • 01. What, Who, Why and Where?
  • 02. Compiled vs Interpreted languages
  • 03. Types of Type!
  • 04. Stack and Heap Memory
  • 05. Garbage Collection
  • 06. About Go
  • 07. Go Playground
  • 08. Hello, World!
  • 09. Installing Go
  • 10. Variables
  • 11. Constants
  • 12. Integers
  • 12. Floats
  • 13. Boolean
  • 14. Strings
  • 15. Complex Numbers
  • 16. If Else
  • 17. Switch
  • 18. For
  • 19. User Defined Types
  • 20. Arrays
  • 21. Slices
  • 22. Structs
  • 23. Maps
  • 24. Functions
  • 25. Defer
  • 26. Pointers
  • 27. Methods
  • Resources
    • Resources - Paid
    • Resources - Free
Powered by GitBook
On this page
  • User Defined Type
  • Declaring our own type

Was this helpful?

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

Previous18. ForNext20. Arrays

Last updated 5 years ago

Was this helpful?