↓ Skip to main content

Scope, Type Conversion and Constants in Go

761 words·4 mins· loading · loading ·
Table of Contents
Learn Golang - This article is part of a series.
Part 6: This Article

Updated 16 Nov 2026: rewritten. The original showed an “implicit conversion” that doesn’t compile and a constant division that gives a different result than it claimed; this version corrects both.

Three ideas that look unrelated cause a lot of early confusion in Go: where a name is visible, how to turn one type into another, and what a constant really is. This post covers all three.

Scope
#

A variable is only visible inside the block where it’s declared. A block is a pair of curly braces: a function body, an if, a for, or a bare { }.

Go doesn’t have a “global” scope in the C sense. It has package scope: a variable declared outside any function is visible to every file in the package. Everything else is local to its block.

Go
package main

import "fmt"

var total = 10 // package scope

func show() {
	fmt.Println("package total:", total)
}

func main() {
	count := 3 // visible only inside main
	fmt.Println("count:", count)
	show()
}
count: 3
package total: 10

A variable declared in an if or for header is limited to that statement:

Go
for i := 0; i < 3; i++ {
	fmt.Println(i)
}
// fmt.Println(i) here would not compile: undefined: i

Shadowing
#

An inner variable can reuse the name of an outer one. The inner one shadows the outer one inside its block, and the outer one is untouched:

Go
func main() {
	total := 99 // a new local variable, not the package-level one
	fmt.Println("local total:", total)
	show()
}
local total: 99
package total: 10

This is the usual trap: you meant to update the package-level variable but wrote := instead of =, which quietly declared a new variable. If a value doesn’t change when you expect it to, check for an accidental :=.

Type conversion
#

Go never converts between numeric types for you. Even int and float64 are different types, so this doesn’t compile:

Go
i := 42
var f float64 = i // error: cannot use i (variable of type int) as float64 value in variable declaration

You convert explicitly with T(value):

Go
i := 42
f := float64(i)
fmt.Println(f)
42

Converting a float to an integer drops the fractional part. It doesn’t round:

Go
price := 3.99
fmt.Println(int(price)) // 3

Converting to a smaller integer type can overflow silently, so check the range first.

One conversion to avoid: string(65) doesn’t give "65". It gives the character with code 65, "A". To turn a number into its text form, use strconv.

Strings and numbers with strconv
#

The strconv package converts between strings and numbers. The functions that parse text return an error, which you should check:

Go
package main

import (
	"fmt"
	"strconv"
)

func main() {
	n, err := strconv.Atoi("42")
	fmt.Println(n, err)

	_, err = strconv.Atoi("abc")
	fmt.Println(err)

	s := strconv.Itoa(12345)
	fmt.Println(s)

	f, _ := strconv.ParseFloat("3.14159", 64)
	fmt.Println(f)

	fmt.Println(strconv.FormatInt(12345, 2))
}
42 <nil>
strconv.Atoi: parsing "abc": invalid syntax
12345
3.14159
11000000111001

Atoi (“ASCII to integer”) and Itoa are the shortcuts for base-10 int values. ParseFloat takes the bit size you want (32 or 64), and FormatInt can print a number in any base, which is why 12345 comes out in binary above.

To see a variable’s type while debugging, use %T with Printf, or reflect.TypeOf from the reflect package.

Constants
#

A constant is a value fixed at compile time, declared with const. It can only hold a number, string or boolean, and you can’t take its address.

Typed and untyped constants
#

A typed constant has a fixed type and follows the same rules as a variable of that type:

Go
const m int = 10
var f float64 = m // compile error: m is an int

An untyped constant has no type until it’s used, so it adapts to its context:

Go
const n = 10
var a int = n
var b float64 = n
fmt.Println(a, b)
10 10

Untyped constants still follow their kind when you do arithmetic on them. 10 is an untyped integer constant, so dividing it by 4 is integer division:

Go
const n = 10
var x float64 = n / 4   // 2: integer division happens first
var y float64 = n / 4.0 // 2.5: 4.0 makes the expression a float
fmt.Println(x, y)
2 2.5

iota
#

iota is a counter that numbers constants in a const block, starting at zero and increasing by one on each line. It’s the usual way to write an enumeration:

Go
type Weekday int

const (
	Sunday Weekday = iota // 0
	Monday                // 1
	Tuesday               // 2
)

fmt.Println(Monday)
1

Next: operators.

Learn Golang - This article is part of a series.
Part 6: This Article

Related