GO Tutorial



DATA TYPES IN GO


Data Types in Go

In Go, data types define the kind of values a variable can hold. Understanding data types helps you write efficient and error-free code.

Common Data Types in Go

Data Type Description Example
int Integer values (whole numbers) 42, -7, 0
float64 Floating-point numbers (decimal values) 3.14, -0.001, 2.0
string Sequence of characters "Hello, Go!"
bool Boolean values (true or false) true, false
byte Alias for uint8, represents raw data 0xFF, 65

Declaring Variables with Data Types

You can explicitly declare a variable’s type using the var keyword, like this:

var age int = 25
var price float64 = 9.99
var name string = "GoLang"
var isActive bool = true
  

Type Inference with := Operator

Go can automatically detect the type based on the assigned value, so you can use the shorthand := for declaration and assignment:

age := 25           // int
price := 9.99       // float64
name := "GoLang"    // string
isActive := true    // bool
  

Interactive Example: Identify Data Types

Below are some variable declarations. Guess the data type before checking the answer!

  • var score = 100 β†’
  • var temperature = 36.6 β†’
  • var greeting = "Hello" β†’
  • var isAvailable = false β†’

Tips for Using Data Types in Go

  • Be explicit: When in doubt, specify the data type to avoid errors.
  • Use type inference: For cleaner code, use := when possible.
  • Know zero values: Go initializes variables with zero values (e.g., 0 for int, "" for string).
  • Type conversion: Use explicit conversion when assigning values between different data types.
Quick Tip: You can find more built-in data types in Go like arrays, slices, maps, and structs. This tutorial covers the basics to get you started!

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review