Menu
Coddy logo textTech

Defining Custom Types

Part of the Fundamentals section of Coddy's GO journey — lesson 94 of 109.

In Go, you can create your own custom types based on existing types. This gives your code more clarity and type safety.

Define a custom type for temperature in Celsius:

type Celsius float64

func main() {
    var temperature Celsius = 25.5
    fmt.Printf("Temperature: %.1f°C\n", temperature)
}

The result shows the temperature value:

Temperature: 25.5°C

Custom types behave like their underlying types but are treated as distinct types by the compiler, improving code readability and preventing logical errors.

challenge icon

Challenge

Beginner

In this challenge, you'll practice defining a custom type in Go. Custom types allow you to create your own data types based on existing ones.

Your task is to define a custom type called Temperature that is based on float64, and then create a variable of this type.

After defining the type, the program should print the value and type of the temperature variable.

Cheat sheet

Create custom types in Go using the type keyword:

type Celsius float64

Custom types are based on existing types but are treated as distinct types by the compiler:

type Celsius float64

func main() {
    var temperature Celsius = 25.5
    fmt.Printf("Temperature: %.1f°C\n", temperature)
}

Custom types improve code readability and provide type safety while behaving like their underlying types.

Try it yourself

package main

import "fmt"

// TODO: Define a custom type called 'Temperature' based on float64

func main() {
	// Create a variable of type Temperature with value 23.5
	var roomTemp Temperature = 23.5
	
	// Print the value and type of roomTemp
	fmt.Printf("Room temperature: %v\n", roomTemp)
	fmt.Printf("Type of roomTemp: %T\n", roomTemp)
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals