Master Go Variables: Types, Declarations, and Best Practices

This article introduces Go language variable fundamentals, explaining why variables are needed, common types, various declaration methods—including explicit typing, type inference, short declaration, global and anonymous variables—and constant usage, complemented by code examples and practical tips for effective Go programming.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Go Variables: Types, Declarations, and Best Practices

Introduction

Hey everyone, I'm Saturday Eight. Let's continue learning Go basics, focusing on variables.

Why Variables Are Needed

Program data is stored in memory, and we cannot reliably use raw memory addresses. Instead, we use variable names to locate the corresponding memory address and manipulate the stored values.

Variable Types

Common variable types across languages include:

Integer (e.g., 1, 6, 2, 8, 4)

Floating‑point (e.g., 1.1, 5.7767, 23.99)

Boolean ( true, false)

String (e.g., "Zhang San", "Li Si")

List

Map

These categories are good to be aware of, though you don't need to memorize them.

Variable Definition

Declaration

Basic syntax:

var variableName variableType</code><code>e.g., var age int</code><code>var gender bool

Batch Declaration

Declare multiple variables at once:

var (</code><code>    age int</code><code>    gender bool</code><code>    name string</code><code>    id int</code><code>    height int</code><code>    ...</code><code>)

Declaration with Assignment

Assign a value when declaring:

var variableName type = value</code><code>e.g., var age int = 18</code><code>var name string = "Zhang San"

Type Inference

Let Go infer the type: var age = 18</code><code>var name = "Zhang San" Here the type is omitted after the variable name.

Short Declaration (:=)

A concise way to declare and assign: age := 18</code><code>name := "Zhang San" Similar to Python's assignment, but requires the colon.

Global Variables

Variables defined outside functions are global. In Go, a global variable must be either declared with a type or declared and assigned in a single statement; the short‑declaration (:=) cannot be used globally.

package main</code><code>// Global variable with type</code><code>var age int</code><code>// Global variable with inferred type</code><code>var age = 18</code><code>// Invalid: separate declaration and assignment</code><code>// var age int</code><code>// age = 18</code><code>// Invalid: short declaration at package level</code><code>// age := 18

Anonymous Variables

Use _ to ignore a returned value when a function returns multiple results:

package main</code><code>func getInfo() (string, int) {</code><code>    return "Zhang San", 18</code><code>}</code><code>func main() {</code><code>    // Standard usage</code><code>    // name, age := getInfo()</code><code>    // fmt.Println(name, age)</code><code>    // Ignoring the first value</code><code>    // name, _ := getInfo()</code><code>    // fmt.Println(name)</code><code>    // Ignoring the second value</code><code>    // _, age := getInfo()</code><code>    // fmt.Println(age)</code><code>}

Anonymous variables do not occupy memory.

Constants

Constants are immutable values, usually defined with uppercase names.

package main</code><code>import "fmt"</code><code>func main() {</code><code>    // Single constant</code><code>    const PI1 int = 3</code><code>    const PI2 = 3.14</code><code>    // Multiple constants</code><code>    const (</code><code>        PI3 = 3.14</code><code>        PI4 = 3.14</code><code>    )</code><code>    // Implicit repetition</code><code>    const (</code><code>        girl1 = 18</code><code>        girl2 // same as previous (18)</code><code>        girl3 // same as previous (18)</code><code>        girl4 = 17</code><code>        girl5 // same as previous (17)</code><code>    )</code><code>    fmt.Println(girl3, girl5)</code><code>}

Note: Constants must be assigned a value at declaration; they cannot be assigned later.

Summary

We have covered Go variables, including their purpose, common types, declaration methods, global variables, anonymous variables, and constants, with practical code examples.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GoType InferenceVariablesConstantsdeclaration
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.