Sling Academy
Home/Golang/How to declare and name variables in Go

How to declare and name variables in Go

Last updated: November 23, 2024

In Go, a programming language developed by Google, variables are a fundamental concept that every programmer needs to master. Variables are used to store information that a Go program can refer to and manipulate. Let’s delve into declaring and naming variables in Go, with examples ranging from basic to advanced.

Basic Variable Declaration

The simplest way to declare a variable in Go is using the var keyword, followed by the variable name and its type:

var name string
var age int

Here, name is a variable that can hold string values, and age is an integer variable.

Variable Initialization

Variables can also be initialized at the time of declaration:

var name string = "Alice"
var age int = 30

This syntax sets name to "Alice" and age to 30. Go also supports type inference, allowing us to omit the type if the variable is being initialized:

var name = "Alice"
var age = 30

Short Variable Declaration

In Go, you can take advantage of a shorthand for declaring and initializing variables using := operator:

name := "Alice"
age := 30

In this form, the type is inferred from the initialized value. This shorthand syntax can only be used within functions.

Multiple Variable Declaration

Go provides a convenient way to declare and initialize multiple variables in one line:

var width, height int = 100, 200

You can also mix different types, but you must use parentheses:

var (
    name   string = "Alice"
    age    int    = 30
    height int    = 64
)

This style is often used for package-level variable declarations.

Advanced Variable Usage

Global variables can be declared using var outside of functions, accessible from anywhere within the package. It's important to keep variable names meaningful and avoid single-character names unless used in concise loops or temporary usages.

Go also allows for blank identifier _, which can ignore values you don’t plan to use:

_, err := io.ReadFull(conn, buf)
if err != nil {
    log.Fatal(err)
}

Here, the blank identifier effectively discards the first value returned by io.ReadFull.

Best Practices for Naming Variables

  • Be descriptive: Use clear and meaningful names for variables.
  • CamelCase for visibility: Start with an uppercase letter for exported variables and a lowercase letter for non-exported variables.
  • Avoid abbreviations unless they are widely accepted (like min and max).
  • Consistency is key: Maintain consistency in naming across your codebase.

With this foundational knowledge, you are ready to correctly declare and name variables in Go, setting yourself up for writing clean and efficient Golang programs.

Next Article: Understanding zero values in Go

Series: Variables & Control Flow

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant