Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page
  • Output
  • Explanation:

Was this helpful?

Edit on GitHub

Variables

This Go code demonstrates variable declaration and initialization using different methods. Let's go through each part of the code:

package main

import "fmt"

func main() {
    // Declare and initialize a variable 'a' with the string value "initial"
    var a = "initial"
    fmt.Println(a)

    // Declare and initialize two variables 'b' and 'c' with integer values 1 and 2
    var b, c int = 1, 2
    fmt.Println(b, c)

    // Declare and initialize a variable 'd' with a boolean value true
    var d = true
    fmt.Println(d)

    // Declare a variable 'e' without initializing it, defaults to the zero value for its type (int in this case)
    var e int
    fmt.Println(e)

    // Short declaration and initialization of a variable 'f' with the string value "apple"
    f := "apple"
    fmt.Println(f)
}

Output

initial
1 2
true
0
apple

Explanation:

  1. var a = "initial": Declares a variable a of type string and initializes it with the value "initial". The type is inferred from the assigned value.

  2. var b, c int = 1, 2: Declares two variables, b and c, both of type int. They are initialized with the values 1 and 2, respectively.

  3. var d = true: Declares a variable d of type bool and initializes it with the value true.

  4. var e int: Declares a variable e of type int without initializing it. In Go, variables that are declared without an explicit initialization are given a zero value for their type. In this case, e will have the zero value for an int, which is 0.

  5. f := "apple": Uses the short declaration syntax to declare and initialize a variable f with the string value "apple". The type is inferred from the assigned value.

The fmt.Println statements are used to print the values of these variables to the console when the program is executed.

PreviousValuesNextConstants

Last updated 1 year ago

Was this helpful?