Types, named types and conversions
Go has a small set of basic types and one way to make more: type Name Base declares a new type with the same representation as the base and a name the compiler treats as distinct. Two values of different named types are not assignable to each other, even when their bits are identical, and that is the point.
A named type
Celsius and Fahrenheit are both float64 underneath, and the compiler still refuses to pass one for the other: a temperature in the wrong unit is a real mistake and the type system catches it for free. A conversion Fahrenheit(x) is the explicit way to say that the change of unit is intended.
package main
import "fmt"
type Celsius float64
type Fahrenheit float64
type UserID int64
func celsiusToFahrenheit(c Celsius) Fahrenheit {
return Fahrenheit(c*9/5 + 32)
}
func main() {
body := Celsius(37)
air := celsiusToFahrenheit(body)
fmt.Printf("%.1f C = %.1f F\n", float64(body), float64(air))
var id UserID = 42
fmt.Printf("%T %v\n", id, id)
fmt.Println("as int64:", int64(id)+1)
}
go run convert.go
37.0 C = 98.6 F
main.UserID 42
as int64: 43
The conversion is not a cast that reinterprets bits: Fahrenheit(c*9/5 + 32) computes a new value. A conversion between numeric types rounds or truncates as the target requires, and a conversion between a string and a slice of bytes copies. %T shows that id is a UserID, and int64(id) is the way back to arithmetic.
Alias against definition
type Alias = int64 is not a new type: it is another name for int64, and the two are interchangeable. The equals sign is the whole difference between a definition and an alias, and it matters for a migration: an alias lets old code keep compiling while a type moves to another package.
package main
import "fmt"
type UserID int64
type Alias = int64
func main() {
var id UserID = 7
var n Alias = 7
fmt.Printf("%T %T\n", id, n)
fmt.Println("same value:", int64(id) == int64(n))
fmt.Printf("id type: %T, alias type: %T\n", id, n)
}
go run alias.go
main.UserID int64
same value: true
id type: main.UserID, alias type: int64
%T prints main.UserID and int64: the defined type has its own name, the alias reports the type it names. The methods lesson adds methods to a defined type, which an alias cannot have.
The conversion everyone gets wrong
Converting an integer to a string does not produce its digits: it produces the character with that code point, because a string is a sequence of bytes and Go treats the conversion as the rune. The function that writes digits is strconv.Itoa.
package main
import (
"fmt"
"strconv"
)
func main() {
var code rune = 65
fmt.Println("string of the rune:", string(code))
fmt.Println("strconv.Itoa:", strconv.Itoa(65))
fmt.Println("conversion int->string is a rune, not a number")
}
go run rune.go
string of the rune: A
strconv.Itoa: 65
conversion int->string is a rune, not a number
string(65) is "A", and "65" comes from strconv. The strings lesson covers the rest of the conversions, including the one from a slice of bytes.
Two named types do not mix
The program below assigns a Celsius to a Fahrenheit without a conversion. The compiler refuses it and names both types, which is the message to keep in mind whenever two quantities of the same shape meet.
package main
import "fmt"
type Celsius float64
type Fahrenheit float64
func main() {
var c Celsius = 20
var f Fahrenheit = c
fmt.Println(f)
}
go build misuse.go
# command-line-arguments
./misuse.go:10:21: cannot use c (variable of float64 type Celsius) as Fahrenheit value in variable declaration
Exercises
- Add a Kelvin type and the two conversions to and from Celsius. How many conversion functions does the program need?
- Change type UserID int64 to type UserID = int64. Which lines of the program can then drop their conversion?
- Print string(rune(0x1F600)) and its length in bytes. Why is the length four?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
type Celsius float64declare?Answer
A new type with the representation of float64
A definition without = creates a distinct named type; an alias with = does not.
-
Can a Celsius value be assigned to a Fahrenheit variable?
Answer
No, a conversion is needed
Named types are distinct even with the same representation.
-
What does
type Alias = int64make?Answer
Another name for int64
The equals sign makes an alias, which is interchangeable with the type it names.
-
What does
string(65)produce?Answer
A
The conversion treats the integer as a code point; strconv.Itoa produces the digits.
-
What does this program print?
package main import "fmt" type ID int func main() { var i ID = 3 fmt.Printf("%T\\n", i) }Answer
main.ID
%T prints the package-qualified name of a defined type.
-
What does this program print?
package main import "fmt" type ID = int func main() { var i ID = 3 fmt.Printf("%T\\n", i) }Answer
int
An alias is not a new type, so the value reports the type it names.
-
What does this program print?
package main import "fmt" type Celsius float64 func main() { c := Celsius(36.6) fmt.Println(int(c)) }Answer
36
A float to integer conversion truncates toward zero.
-
What does the compiler say about assigning a Celsius to a Fahrenheit?
Answer
cannot use c (variable of float64 type Celsius) as Fahrenheit value
The message names both types and the variable; the fix is an explicit conversion.
-
Which conversion copies a string into a slice of bytes?
Answer
[]byte(s)
A string is immutable, so the conversion copies its bytes into a new slice.
-
Why is a named type worth the extra name?
Answer
It stops two quantities of the same shape from mixing
The compiler enforces the distinction, so a unit mistake is a build error.
The code on this page was run with go 1.26.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev