Simple Custom Types
You can create custom types using Go's simple types as a starting point. The notation is type <name> <type>
. If we were to create an ID type based on a string, this would look like type id string
. The custom type acts the same as the type you based it on, including getting the same zero value and having the same abilities to compare with other values of the same type. A custom type is not compatible with its base type, but you can convert your custom type back into the type it's based on to allow for interaction.
Exercise 4.16: Creating a Simple Custom Type
In this exercise, we'll define a map and then delete an element from it using user input. Then, we'll print the now possibly smaller map to the console. Let's get started:
- Create a new folder and add a file named
main.go
to it. - In
main.go
, add the package and imports:package main import "fmt"
- Define a custom type called
id
based on the string...