Variables and Types in Go

ยท

3 min read

Variables and Types in Go

Photo by Chinmay B on Unsplash

Go is a statically typed programming language that places a strong emphasis on type safety and explicit variable declarations. In this blog post, we'll dive deep into variables and types in Go, exploring how they work and best practices for using them effectively.

List of types:

  • bool

  • string

  • Signed Integer

    • int

    • int8

    • int16

    • int32

    • int64

  • Unsigned Integer

    • uint

    • uint8

    • uint16

    • uint32

    • uint64

    • uintptr

  • byte //alias for uint8

  • rune // alias for int32

  • float32

  • float64

  • complex64

  • complex128

NOTE: int, uint and uintptr consists 32 bits for 32-bit systems and 64 bits for 64-bit systems.

1. Explicit Type Declaration

var name string
var age int
var price float64

This method explicitly declares the variable type. If no initial value is provided, Go initializes the variable with a zero value:

  • Strings are initialized to ""

  • Integers to 0

  • Floats to 0.0

  • Boolean to false

  • Pointers to nil

2. Type Inference

name := "John Doe"  // Go infers type as string
age := 30           // Go infers type as int
isStudent := true   // Go infers type as bool

The := operator is a shorthand for declaring and initializing variables. It can only be used inside functions.

3. Multiple Variable Declaration

var x, y int = 10, 20
firstName, lastName := "John", "Doe"

Go allows declaring multiple variables in a single line, which can make your code more concise.

Numeric Types

  • Integers

      var smallInt int8 = 127     // 8-bit signed integer
      var unsignedInt uint = 255  // unsigned integer
      var bigInt int64 = 9223372036854775807  // 64-bit integer
    
  • Floating-Point Types

      var precision32 float32 = 3.14  // 32-bit floating-point
      var precision64 float64 = 3.141592653589793  // 64-bit floating-point
    

Boolean Type

var isTrue bool = true
var isFalse bool = false

String Type

var greeting string = "Hello, Go!"
name := "Gopher"  // Using type inference

Complex Types

var complex64Val complex64 = 3 + 4i
complexNumber := 5 + 6i  // Type inference

Type Conversion

Go requires explicit type conversion. You can't automatically convert between types:

var intValue int = 42
var floatValue float64 = float64(intValue)  // Explicit conversion

// This would cause a compilation error
// var floatValue float64 = intValue

Constants

Constants in Go are declared using the const keyword:

const Pi = 3.14159
const MaxUsers = 100
const AppName = "GoApp"

Constants are immutable and can be character, string, boolean, or numeric values.

Best Practices

  1. Use type inference (:=) when the type is obvious

  2. Use explicit type declaration when you want to be clear about the type

  3. Always initialize variables before using them

  4. Use the most appropriate integer type for your use case

  5. Be explicit about type conversions

Common Pitfalls to Avoid

  • Don't mix types without explicit conversion

  • Be careful with integer overflow

  • Understand zero values for different types

  • Use appropriate types for your specific use case

ย