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°CCustom types behave like their underlying types but are treated as distinct types by the compiler, improving code readability and preventing logical errors.
Challenge
BeginnerIn 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.
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)
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions10Composite Types: Arrays
Introduction to ArraysDeclaring ArraysInitializing ArraysAccessing Array ElementsArray Length with `len`Iterating Over ArraysMulti-dimensional Arrays13Composite Types: Structs
Defining Custom TypesCreating Struct InstancesAccessing Struct FieldsPointers to StructsInitializing StructsEmbedded StructsAnonymous StructsRecap - Custom Data Structures2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions