Go Variable Scope

Introduction

Go variable scope is based on code blocks.

Inner block can access the variables defined inside outer block.

Outer block cannot access the variables defined inside inner block .

package main /*  w w  w  .  j  av a2s  .co m*/

import (
  "fmt"
)

var s = "CSS"

func main() {
  fmt.Println(s)
  x := true

  if x {
    y := 1
    if x != false {
      fmt.Println(s)
      fmt.Println(x)
      fmt.Println(y)
    }
  }
  fmt.Println(x)
}



PreviousNext

Related