Variables in Go (Golang)

DiwakarDiwakar
2 min read

In Go (commonly known as Golang), variables are a fundamental concept. Variables are used to store data that can be used and manipulated throughout a program. Here's a breakdown of variables in Go:

  1. Declaring Variables:

    You can declare a variable using the var keyword.

     var x int
    

    This declares a variable named x of type int. You can also assign a value during declaration.

     var y int = 10
    

    For multiple variables:

     var a, b, c int
    
  2. Short Declaration:

    Go supports a shorthand syntax to declare and initialize a variable.

     z := 20
    

    This infers the type of z based on the value you assign to it.

  3. Zero Values:

    In Go, variables declared without an explicit initial value are given their zero value. The zero value is:

  • 0 for numeric types

  • false for the boolean type

  • "" (empty string) for strings

  • nil for pointers, functions, interfaces, slices, channels, and maps.

4- Constants:

Go supports constants, which are declared like variables but with the const keyword. Constants cannot be changed after they're declared.

const Pi = 3.14159

5- Data Types:

Go is a statically-typed language, which means the variable type is determined at compile time. Some common types include:

  • Basic Types: - int, int8, int16, int32, int64 - uint, uint8, uint16, uint32, uint64, uintptr - float32, float64 - complex64, complex128 - bool - string

  • Composite Types: - array - slice - map - struct - channel - interface - pointer - function

6- Pointers:

A pointer is a variable that stores the address of another variable. Pointers in Go are similar to those in other languages like C and C++.

var ptr *int

This declares a pointer to an integer.

7- Scope:

Variables can be defined in various scopes:

  • Local: Inside a function or block, only accessible within that function.

  • Package: Outside a function but inside a package, accessible throughout the package.

  • Global: Declared outside a function, accessible throughout the program.

  • Exported/Unexported: Variables starting with an uppercase letter are exported and can be accessed from other packages. Those with lowercase are unexported and are private to their package.

Using variables efficiently and understanding their scope and lifetime is crucial when writing Go programs.

Thank you for reading. I encourage you to follow me on Twitter where I regularly share content about JavaScript and React, as well as contribute to open-source projects and learning golang. I am currently seeking a remote job or internship.

Twitter: https://twitter.com/Diwakar_766

GitHub: https://github.com/DIWAKARKASHYAP

Portfolio: https://diwakar-portfolio.vercel.app/

0
Subscribe to my newsletter

Read articles from Diwakar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Diwakar
Diwakar

As a passionate developer, I thrive on acquiring new knowledge. My journey began with web development, and I am now actively engaged in open source contributions, aiding individuals and businesses.