HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to GoVariablesData TypesOperatorsStringsArray & SliceMap (Dictionary)Conditions (if / switch)Loops (for)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & Slice
🗂️ Map
🔁 Condition & Loop
🧩 Function
Go›Data Types

Data Types

int, float64, string, bool types; finding the type with %T and type conversion.

Simple enough for a 5-year-old to understand

Basic types

Example code
1
package main
2
import "fmt"
3
func main() {
4
var number int = 42
int — integer.
Variableint — integer.
5
var decimal float64 = 1.72
float64 — decimal number.
Variablefloat64 — decimal number.
6
fmt.Printf("%T %T\n", number, decimal)
Prints the type with %T: int float64
Output / InputPrints the type with %T: int float64
7
}
VariableOutput / Input
💡 Hint: Go is statically typed: the type of a variable is fixed and checked by the compiler.

About this topic

Go's type system is explicit and predictable. In numeric types, the size is written in the name: int8, int32, int64, uint8, and so on. The unsized int is the platform's natural word size and is the default choice in everyday code.

Go's strictest rule appears right here: THERE IS NO IMPLICIT TYPE CONVERSION. int and int64 are different types, and to assign one to the other, you must explicitly convert them. It seems unnecessary at first; in return, overflow and loss of precision never happen silently.

Strings in Go are byte slices and are UTF-8 encoded. That's why len returns the number of BYTES in a string, not the number of characters. This distinction is critical when working with Turkish characters.

What you will learn

  • Numeric types and their sizes
  • The necessity of explicit type conversion
  • The rune type and its relationship with UTF-8
  • Integer division behavior
  • Conversion from string to number and error handling
  • Defining a type alias

When to use?

Unsized int is almost always the right choice. A specific size is only needed when dealing with binary protocols, file formats, or memory constraints.

The strconv package is used for string-to-number conversion and returns TWO values: the result and an error. Checking the error in Go is not an optional nicety, but a core idiom of the language.

Common mistakes

Wrong

var a int = 5
var b int64 = 10
fmt.Println(a + b)

Go does not perform implicit conversion even between int and int64; you get the "invalid operation: a + b (mismatched types int and int64)" error. The conversion must be written explicitly.

Correct

var a int = 5
var b int64 = 10
fmt.Println(int64(a) + b)

Wrong

s := "Tree"
fmt.Println(len(s))

len counts BYTES; since strings are UTF-8 encoded, Turkish characters take up two bytes, resulting in 6 instead of 4. For character count, utf8.RuneCountInString should be used.

Correct

s := "Tree"
fmt.Println(utf8.RuneCountInString(s))

Wrong

n, _ := strconv.Atoi("abc")
fmt.Println(n + 1)

Since the error is discarded with an underscore, you won't notice that the conversion failed; n remains zero and the calculation is silently wrong. Every call in Go that returns an error must have that error checked.

Correct

n, err := strconv.Atoi("abc")
if err != nil {
	fmt.Println("invalid number")
	return
}
fmt.Println(n + 1)

Frequently asked questions

Should I use int or int64?

Use int unless you have a specific reason. It is the platform's natural size and the most efficient. A specific size is only chosen for binary data formats, network protocols, or when you need to guarantee the exact range of a number.

What is a rune?

It is the type that holds a single Unicode code point, technically an alias for int32. To iterate over a string character by character, you convert it to a rune slice; iterating byte by byte splits Turkish characters apart.

Which type should I use for money calculations?

Integers in cents — meaning storing the amount as int64 in the smallest unit. float64 stores decimals approximately, and penny losses accumulate. Go's standard library does not have a decimal type, so the integer approach is common practice.

Summary

Go has no implicit type conversion; even between int and int64, an explicit conversion is required. Strings are UTF-8 byte slices, so len counts bytes, and the error of strconv calls must always be checked.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code