Skip to content
Justice Tech School

Custom Types

type Celsius float64 creates a new type with its own methods. Defined types make code safer because Celsius and Fahrenheit cannot be mixed by accident.

Example
package main

import "fmt"

type Celsius float64
type Fahrenheit float64

func (c Celsius) ToF() Fahrenheit { return Fahrenheit(c*9/5 + 32) }

func main() {
	fmt.Println(Celsius(100).ToF())
}

Result

212

Exercise

Declare a new type.

 Celsius float64

Quiz Test

Can defined types have methods?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.