← Back to Go Course | Chapter 2: Variables & Types | Lesson 4 of 7

Basic Data Types

Go's basic types are the different kinds of boxes you can choose from: whole numbers, decimals, true/false, and text.
Syntax
go
var name int      // int, int8..int64, uint, uintptr
var name float64  // float32, float64
var name bool
var name string

Integers

Go provides both a general-purpose int (whose size matches the platform, 32 or 64 bit) and explicitly sized integer types like int8, int16, int32, and int64, plus unsigned versions (uint8, uint32, etc.) for values that are never negative.

Note: Use plain int unless you have a specific reason (like memory layout or binary formats) to pick a sized variant.

Example: Integers

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Declare int and set it to 42
	var count int = 42
	// Declare int8 and set it to 120
	var small int8 = 120
	var big int64 = 9000000000
	// Print the values followed by a newline
	fmt.Println(count, small, big)
}

Floating-Point Numbers

float32 and float64 represent numbers with a decimal point. float64 is the default and generally preferred for precision unless memory is tightly constrained, since float32 loses accuracy for very large or very precise values.

Example: Floating-Point Numbers

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Declare float64 and set it to 19.99
	var price float64 = 19.99
	// Declare float32 and set it to 0.15
	var discount float32 = 0.15
	// Print the values followed by a newline
	fmt.Println("price:", price, "discount:", discount)
}

Booleans

The bool type holds exactly one of two values, true or false, and is the result of every comparison operator (==, <, >, etc.). Booleans control branching logic in if statements and loop conditions.

Example: Booleans

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Short declaration: create isActive and infer the type
	isActive := true
	// Short declaration: create hasExpired and infer the type
	hasExpired := false
	// Print the values followed by a newline
	fmt.Println("active:", isActive, "expired:", hasExpired)
}

Strings

A string in Go is an immutable sequence of bytes, most commonly holding UTF-8 encoded text. String literals are written in double quotes, and Go provides many built-in operators and standard-library functions for working with them.

Example: Strings

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Short declaration: create greeting and infer the type
	greeting := "Namaste"
	// Print the values followed by a newline
	fmt.Println(greeting, "length in bytes:", len(greeting))
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using int everywhere and assuming it's always 64-bit -- its size actually depends on the target platform (32 or 64 bit).
  2. Mixing float32 and float64 values in the same expression without an explicit conversion, which Go's type system rejects.
  3. Comparing floating-point numbers with == for equality when rounding error can make two equal values differ slightly.
Chapter Summary
  • Go's core basic types include int, float64, bool, and string.
  • Integer types come in sized variants like int8, int16, int32, int64 and their unsigned counterparts.
  • float32 and float64 represent decimal numbers, with float64 the common default.
  • Go is strict: it never silently mixes types in an expression without an explicit conversion.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.