Start learning free

LESSON 6 OF 338 · WELCOME TO GO

Name Errors in Go: Fixing undefined Identifiers

Lesson Read the full explanation

TL ; DR:

  • A name error happens when Go sees an identifier that does not exist.

  • Misspelling a function name causes an error:

    fmt.Prntln("Hey!")
    
  • Misspelling a variable name also causes an error:

    var animal string = "parrot"
    fmt.Println(anmal)
    



Full lesson:

Go must know what every identifier refers to. If a function or variable name is misspelled, Go cannot compile the program.

For example, fmt.Println exists, but fmt.Prntln does not.

Variable names must match exactly too:

var weather string = "sunny"
fmt.Println(weather)

Assignment
Follow the Coding Tutorial and fix the misspelled names.


Hint
Check the function name and the variable name.


STEP 1 OF 1
    Ctrl + Enter to check

    GO · WELCOME TO GO

    Name Errors in Go, explained

    What a name error is

    Every name in a Go program has to refer to something: a variable you declared, a function you defined, or something from a package you imported. When Go meets a name it can't find, it reports an undefined error and refuses to build the program. Nothing runs, not even the lines before the mistake.

    These errors are the most common ones beginners hit, and they nearly always come from a typo. The compiler is not guessing what you meant. It compares names character by character, so one wrong letter is a different name.

    Two kinds of misspelling

    A misspelled function from a package

    fmt.Println exists. fmt.Prntln does not. Go reports the name together with the package it expected to find it in:

    ./prog.go:6:6: undefined: fmt.Prntln
    

    A misspelled variable

    The same thing happens with variables. Here the variable is declared as weather but printed as whether:

    var weather string = "sunny"
    fmt.Println(whether)
    

    Go has never heard of whether, so it reports undefined: whether. It also reports weather declared and not used, because the real variable is never read. Go treats an unused local variable as an error too. So one typo can produce two messages, and fixing the spelling clears both.

    Names are case-sensitive

    Go distinguishes upper and lower case, so fmt.println is also undefined. In Go, a package exports only names that start with a capital letter, and the function in fmt is Println, with a capital P. Variables follow the same rule: weather and Weather are different names.

    var weather string = "sunny"
    fmt.Println(Weather) // undefined: Weather
    

    Other causes of undefined

    • Using a variable before declaring it. Go reads a function from top to bottom, so a name is only defined after its declaration line.
    • Using a variable outside its block. A variable declared inside an if or a for exists only in that block.
    • A missing import. Calling strconv.Itoa without importing "strconv" gives undefined: strconv.

    Here is a short function that shows all three. The compiler's messages are below it; their line numbers count from the top of the whole file, which also has the package line and the import.

    func main() {
    	fmt.Println(total)
    	total := 5
    	if total > 1 {
    		inner := 1
    	}
    	fmt.Println(inner, strconv.Itoa(total))
    }
    
    ./prog.go:6:14: undefined: total
    ./prog.go:9:3: inner declared and not used
    ./prog.go:11:14: undefined: inner
    ./prog.go:11:21: undefined: strconv
    

    Notice that the first message is about line 6, even though total is declared on line 7. At line 6 the name doesn't exist yet.

    How to read the message

    The compiler output has the form file:line:column: message. The line number points at the exact place where the unknown name appears. Go through the list one error at a time: read the name in the message, then compare it letter by letter with the spelling in the declaration. If you copy a name from the declaration into the failing line, the typo disappears.

    The exercise, step by step

    The starter code has two lines that Go rejects. Together they should print Welcome! and then sunny.

    1. Line 1. The call is fmt.Prntln("Welcome!"). The function in the fmt package is spelled Println, so restore the missing i.
    2. Line 4. The call prints whether, but line 3 declares weather. Change the argument to weather.

    Everything else stays as it is, including the declaration on line 3. The fixed code, as in the lesson:

    fmt.Println("Welcome!")        // this line produces an error
    
    var weather string = "sunny"
    fmt.Println(weather)          // this line produces an error
    

    The comments are left over from the starter code. They are only comments now, because both lines are correct, and they have no effect on the program. The output is:

    Welcome!
    sunny
    

    Key takeaways

    • An undefined error means Go can't find a declaration for the name.
    • Go compares names exactly, including letter case.
    • One typo in a variable name can also trigger declared and not used.
    • The error points at a file, line and column: start at that spot and compare the name with its declaration.
    • If the program has any error, nothing runs at all.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free