Go (Golang) Basic – Data Types and Variables

The Building Blocks of a Program

To build any useful program, we need a way to store and manage information—a user’s name, a score in a game, or the price of a product. In programming, we do this using variables and data types.

They are the…


This content originally appeared on DEV Community and was authored by Andi

The Building Blocks of a Program

To build any useful program, we need a way to store and manage information—a user's name, a score in a game, or the price of a product. In programming, we do this using variables and data types.

They are the most fundamental building blocks for handling data. Let's dive into how Go approaches this.

What Are Data Types? (Why Go is "Strict")

Before we can store a piece of data, we must tell Go what kind of data it is. Go is a statically-typed language, which means it's very strict about this rule.

Think of it like having labeled storage boxes: one for "Books", one for "Clothes", and one for "Toys". You can't put a toy in the book box. This strictness helps prevent many common bugs right from the start.

Here are the four most essential data types in Go:

  • string: Used for text. Any sequence of characters enclosed in double quotes (").

    • Example: "Hello, Budi", "123 Main St."
  • int: Used for integers (whole numbers), both positive and negative.

    • Example: 10, -50, 2025
  • float64: Used for floating-point numbers (decimals).

    • Example: 3.14, 99.5
  • bool: Used for boolean logic (true or false).

    • Example: true, false

Variables: The Containers for Your Data

A variable is simply a named container for our data. Once you create (declare) a variable, you can change its value later. There are two common ways to declare a variable in Go:

1. The Formal Way (var)

This way is very explicit about the variable's type.

// var <variableName> <dataType> = <value>
var age int = 30

Go can also infer the type if you leave it out:

var city = "Jakarta" // Go knows this is a string

2. The Short Way (:=)

This is the most common and preferred way to declare and initialize a variable inside a function. It's clean and concise. This is the most common and preferred way to declare and initialize a variable inside a function.

// <variableName> := <value>
name := "Siti"
isActive := true

Once a variable is declared, you can update its value using a simple equals sign (=):

name = "Siti Nurbaya" // Updating the value

Constants: Values That Never Change

Sometimes, you have a value that you know will never, ever change throughout your program's life. Think of mathematical constants like Pi, or a specific configuration setting. For these, Go gives us constants.

A constant is declared using the const keyword. Once set, its value is locked and cannot be changed.

const pi = 3.14
const defaultPort = 8080

// This would cause an error! You cannot change a constant.
// pi = 3.15 

Putting It All Together: A Code Example

Let's see all these concepts in action in a single program. Create a main.go file and try out this code:

package main

import "fmt"

func main() {
    // --- STRINGS ---
    // Using the short declaration (:=)
    productName := "Go Programming E-book"
    fmt.Println(productName)

    // --- INTEGERS ---
    // Using the formal declaration (var)
    var quantity int = 10
    var price = 50000 // Go infers this is an int
    fmt.Println(quantity)
    fmt.Println(price)

    // --- FLOATS ---
    var score float64 = 85.5
    fmt.Println(score)

    // --- BOOLEANS ---
    isPublished := true
    fmt.Println("Is the product published?", isPublished)

    // --- CONSTANTS ---
    const author = "Go Community"
    fmt.Println("Author:", author)

    // --- UPDATING A VARIABLE ---
    fmt.Println("Current quantity:", quantity)
    quantity = 15 // Update the value
    fmt.Println("Updated quantity:", quantity)
}

Conclusion

You've now mastered the absolute fundamentals of handling data in Go. You know how to store text, numbers, and logical values using variables, and how to protect important values using constants.

These are the essential building blocks we'll use in every program from now on. In the next part of the series, we'll take a big step forward and learn how to store collections of data using two of Go's most powerful features: Slices and Maps. See you there!


This content originally appeared on DEV Community and was authored by Andi


Print Share Comment Cite Upload Translate Updates
APA

Andi | Sciencx (2025-09-28T06:57:32+00:00) Go (Golang) Basic – Data Types and Variables. Retrieved from https://www.scien.cx/2025/09/28/go-golang-basic-data-types-and-variables/

MLA
" » Go (Golang) Basic – Data Types and Variables." Andi | Sciencx - Sunday September 28, 2025, https://www.scien.cx/2025/09/28/go-golang-basic-data-types-and-variables/
HARVARD
Andi | Sciencx Sunday September 28, 2025 » Go (Golang) Basic – Data Types and Variables., viewed ,<https://www.scien.cx/2025/09/28/go-golang-basic-data-types-and-variables/>
VANCOUVER
Andi | Sciencx - » Go (Golang) Basic – Data Types and Variables. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/28/go-golang-basic-data-types-and-variables/
CHICAGO
" » Go (Golang) Basic – Data Types and Variables." Andi | Sciencx - Accessed . https://www.scien.cx/2025/09/28/go-golang-basic-data-types-and-variables/
IEEE
" » Go (Golang) Basic – Data Types and Variables." Andi | Sciencx [Online]. Available: https://www.scien.cx/2025/09/28/go-golang-basic-data-types-and-variables/. [Accessed: ]
rf:citation
» Go (Golang) Basic – Data Types and Variables | Andi | Sciencx | https://www.scien.cx/2025/09/28/go-golang-basic-data-types-and-variables/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.