View : 221

25/04/2026 02:47am

EP.2 Introduction to Variables and Data Types in Go - Fundamentals You Need to Know!

EP.2 Introduction to Variables and Data Types in Go - Fundamentals You Need to Know!

#Coding Fundamentals

#Go Programming

#Go language

#Data Types in Go

#Variables in Go

Introduction to Variables and Data Types in Go - Fundamentals You Need to Know!

Variables - What are Variables? Variables are storage locations in a program for data, such as numbers, text, or various values that we want to use in our code. In Go, we declare variables using the keyword var or by using a shorthand method, which is :=

var x int = 10 // ประกาศตัวแปรแบบกำหนดชนิดข้อมูล
name := "Go" // แบบสั้น ไม่ต้องระบุชนิดข้อมูล

 

Data Types - Data Types in Go In Go, we need to specify the data type for variables, such as:

  • int: stores integers
  • float64: stores floating-point numbers
  • string: stores text
  • bool: stores boolean values (True/False)

Go is a strongly-typed language (requiring explicit data type declaration) for safety and to prevent programming errors.

Example

var age int = 25
var height float64 = 5.9
isStudent := true

 

Displaying Variables Try writing a program that declares multiple variable types and outputs them.

package main

import "fmt"

func main() {
    var age int = 25
    name := "Alice"
    fmt.Println("Name:", name)
    fmt.Println("Age:", age)
}

 

Using var If you want to specify the data type, you can use the keyword var, but if you want to write shorter and more concise code, just use := and Go will automatically infer the data type for you!