Go Variables

Gayathri. B

 Go Variable Types:

Go supports different types of variables to store various kinds of data. Here are some common types:

  • int – Used to store whole numbers, such as 123 or -123.
  • float32 – Used for numbers with decimal points, like 19.99 or -19.99.
  • string – Used to store text values, for example "Hello World". Strings are enclosed in double quotes.
  • bool – Used for boolean values, representing either true or false.

You will learn more about these and other variable types in the upcoming Go Data Types chapter.

Declaring Variables in Go

  • In Go, variables can be declared in two main ways:

1.Using the var keyword:

  • Declare a variable by writing the var keyword, followed by the variable name and its type.
Syntax:
  • To declare a variable in Go, use the following format:
        var variableName type = value
Here:
  • var is the keyword used to declare a variable
  • variableName is the name of the variable
  • type specifies the data type (e.g., int, string, float32, etc.)
  • value is the initial value assigned to the variable
Note: You must always specify either the type, the value, or both when declaring a variable.

2. Using the := Operator

  • You can declare and initialize a variable in one step using the := shorthand.
Syntax:
        variablename := value
  • In this form, the variable's type is automatically inferred by the compiler based on the assigned value.
  • Important: You must assign a value when using :=; it cannot be used for declaration only.

Declaring Variables with Initial Values

  • When you already know the value of a variable at the time of declaration, you can assign it immediately in a single line.

Example:

package main
import ("fmt")
func main() {
  var student1 string = "John"  // Explicit type declaration
  var student2 = "Jane"         // Type is inferred by the compiler
  x := 2                        // Short declaration with inferred type
  fmt.Println(student1)
  fmt.Println(student2)
  fmt.Println(x)
}

Note: The types of student2 and x are automatically inferred based on the values assigned to them.

Declaring Variables Without Initial Values in Go

In Go, every declared variable is automatically assigned a default zero value based on its type. So, when a variable is declared without an initial value, it gets initialized with this default:
  • string → "" (empty string)
  • int → 0
  • bool → false

Example:

package main
import "fmt"
func main() {
  var a string  // default is ""
  var b int     // default is 0
  var c bool    // default is false
  fmt.Println(a)
  fmt.Println(b)
  fmt.Println(c)
}

Explanation of the Example

In this example, three variables are declared:
  • a
  • b
  • c
These variables are declared without assigning any initial values. However, in Go, all variables are automatically initialized with default values based on their types.
When the code is executed, the default values are:
  • a (string) → "" (empty string)
  • b (int) → 0
  • c (bool) → false
This demonstrates that Go assigns a default value even if no explicit value is provided during declaration.

Assigning Value After Declaration

  • In Go, you can assign a value to a variable after it has been declared. This is useful when the initial value is not known at the time of declaration.

Example:

package main
import ("fmt")
func main() {
  var student1 string  // Declaration without initial value
  student1 = "John"    // Assigning value later
  fmt.Println(student1)
}
  • In this example, the variable student1 is first declared as a string, and the value "John" is assigned to it afterward.
Note: Variables cannot be declared using := without providing an initial value.




Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send