0
0
Goprogramming~5 mins

Basic data types in Go

Choose your learning style9 modes available
Introduction

Basic data types help us store different kinds of information like numbers, text, and true/false values in a program.

When you want to store a person's age as a number.
When you need to save a name or message as text.
When you want to keep track if a light is on or off using true or false.
When you want to do math with numbers like adding or subtracting.
When you want to check conditions like if a user is logged in or not.
Syntax
Go
var variableName dataType

// Examples:
var age int
var name string
var isActive bool

Use var keyword to declare a variable with a type.

Common basic types are int for whole numbers, float64 for decimal numbers, string for text, and bool for true/false.

Examples
This creates a variable named age that holds a whole number 30.
Go
var age int = 30
This creates a variable named price that holds a decimal number 19.99.
Go
var price float64 = 19.99
This creates a variable named name that holds the text "Alice".
Go
var name string = "Alice"
This creates a variable named isOpen that holds the value true.
Go
var isOpen bool = true
Sample Program

This program declares variables of different basic types and prints their values.

Go
package main

import "fmt"

func main() {
    var age int = 25
    var price float64 = 9.99
    var name string = "Bob"
    var isMember bool = false

    fmt.Println("Age:", age)
    fmt.Println("Price:", price)
    fmt.Println("Name:", name)
    fmt.Println("Is member:", isMember)
}
OutputSuccess
Important Notes

Go requires you to specify the type of variable when you declare it.

You can also let Go guess the type by using := like age := 25.

Basic types are the building blocks for storing data in your program.

Summary

Basic data types store simple values like numbers, text, and true/false.

Use int for whole numbers, float64 for decimals, string for text, and bool for true/false.

Declare variables with var and specify the type to hold data.