Skip to main content

Introduction

Go has various value types that form the foundation of all programs. Understanding these basic types - strings, integers, floats, and booleans - is essential for writing Go code.

The Code

// Go has various value types including strings,
// integers, floats, booleans, etc. Here are a few
// basic examples.

package main

import "fmt"

func main() {

	// Strings, which can be added together with `+`.
	fmt.Println("go" + "lang")

	// Integers and floats.
	fmt.Println("1+1 =", 1+1)
	fmt.Println("7.0/3.0 =", 7.0/3.0)

	// Booleans, with boolean operators as you'd expect.
	fmt.Println(true && false)
	fmt.Println(true || false)
	fmt.Println(!true)
}

Value Types in Detail

Strings

fmt.Println("go" + "lang")
Strings can be concatenated using the + operator. This example outputs golang.
Strings in Go are immutable - once created, their contents cannot be changed. Operations like concatenation create new strings.

Integers and Floats

fmt.Println("1+1 =", 1+1)
fmt.Println("7.0/3.0 =", 7.0/3.0)
Go supports standard arithmetic operations:
  • Integer arithmetic: 1+1 produces 2
  • Float arithmetic: 7.0/3.0 produces 2.3333333333333335
Integer division truncates: 7/3 would give 2, but 7.0/3.0 gives the full floating-point result.

Booleans

fmt.Println(true && false)  // false
fmt.Println(true || false)  // true
fmt.Println(!true)          // false
Booleans support standard logical operators:
  • && - AND operator
  • || - OR operator
  • ! - NOT operator

Common Value Types

Go provides several built-in value types:
TypeExamplesDescription
string"hello", "golang"Text data
int42, -7, 1000Whole numbers
float643.14, 7.0, -2.5Decimal numbers
booltrue, falseBoolean values
Go has multiple integer types (int8, int16, int32, int64) and float types (float32, float64). The plain int type is the most commonly used and is sized based on your system (32 or 64 bits).

Key Takeaways

  • Strings are concatenated with +
  • Go distinguishes between integer and floating-point arithmetic
  • Boolean operators work as expected from other languages
  • Go is a statically typed language - each value has a specific type
  • Use fmt.Println to output values of any type

Build docs developers (and LLMs) love