Types

Contents
Basic types
Zero values table
Conversions
Inspect with reflect

Basic types

Go programs are built from a small set of basic types: bool for logic, string for text, integers such as int, and floats such as float64.

Pick the type that matches the data: bool for flags, int for counts, float64 for measurements, and string for names and messages.

package main import "fmt" func main() { var active bool = true var title string = "course" var count int = 7 var ratio float64 = 0.5 fmt.Println(active, title, count, ratio) }

go run .

true course 7 0.5

Zero values table

Each type has a fixed zero value used before the first assignment.

Numbers start at 0, floats at 0.0, booleans at false, strings at the empty string, and composite types such as pointers and slices start at nil.

Knowing the table removes surprises: an unset counter reads as 0 and an unset flag reads as false.

Conversions

Go never converts types implicitly, so every conversion is written out with the target type or a helper from the strconv package.

Numeric conversions use the plain type name, while text conversions use Atoi, Itoa, and ParseFloat and report errors for bad input.

package main import ( "fmt" "strconv" ) func main() { n, err := strconv.Atoi("42") text := strconv.Itoa(42) asFloat := float64(n) fmt.Println(n, text, asFloat, err) }

go run .

42 42 42 <nil>

Inspect with reflect

When the type is unclear, the reflect package reports it at runtime with the TypeOf function.

The step-by-step examples live in the article inspect variable types, and the snippet below shows the core idea.

package main import ( "fmt" "reflect" ) func main() { age := 5 title := "course" fmt.Println(reflect.TypeOf(age)) fmt.Println(reflect.TypeOf(title)) }

go run .

int
string

Next: Numbers

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: