[Solved] How to Fix the Error .\main.go: undefined in Golang

In this tutorial, we will learn about “how to fix the error .\main.go: undefined in Golang”. If you are a Golang developer, you must have encountered this error during compilation or execution. If you are new to Golang, this error might be on your way or might have hit you already. This error mostly indicate that the Go compiler is unable to locate a definition for a variable, function or a package used in the main.go file. We will learn more about the scenarios where this error occurs followed by some of the ways to fix the error. So let us begin the tutorial.

 

Why the Error Occured?

In Golang, the error “.\main.go: undefined in Golang” occurs when the Go compiler cannot find the definition for a variable or function that is being used in the code. A very common scenario where this error can potentially occur is when you create multiple golang files in the same package and have a common main.go file. In such case, when you execute the main.go file, the Go compiler will not be able to refer to any of the variable or functions defined in any of the file which has the reference in main.go file. To get a more clear picture of this, let us understand this by taking an example in the next section.

 

How to Reproduce the Error

I would like to mention that I have used Windows operating system and VS Code IDE for this tutorial. You can use any other IDE or even any other Operating System to execute the commands given in this tutorial but it may or may not work on other operating systems. Now coming back to the topic, to reproduce the error, let us consider an example. We will create a package inside which, we will create three files. These files are:

square.go:        defines a function CalculateSquareArea() to calculate the area of square.
rectangle.go:   defines a function CalculateRectangleArea() to calculate the area of rectangle.
main.go:          defines the main function from where CalculateSquareArea() and CalculateRectangleArea() functions are called.

I have provided the content of each .go file below. For the simplicity, Copy the code and save all three files in the same package.

square.go

package main

// CalculateSquareArea calculates the area of a square 
func CalculateSquareArea(sideLength float64) float64 {

    return sideLength * sideLength
}

 

rectangle.go

package main

// CalculateRectangleArea calculates the area of a rectangle 
func CalculateRectangleArea(length, width float64) float64 {

    return length * width
}

 

main.go
package main

import "fmt"

func main() {

    // Sample values for demonstration
    squareSide := 5.0
    rectangleLength := 4.0
    rectangleWidth := 6.0

    // Calculate and print the area of a square
    squareArea := CalculateSquareArea(squareSide)
    fmt.Printf("Area of Square with side %.2f: %.2f\n", squareSide, squareArea)

    // Calculate and print the area of a rectangle
    rectangleArea := CalculateRectangleArea(rectangleLength, rectangleWidth)
    fmt.Printf("Area of Rectangle with length %.2f and width %.2f: %.2f\n", rectangleLength, rectangleWidth, rectangleArea)
}

 

Let us now execute the main.go file and see the output it returns. As you see below, it will throw the error saying undefined CalculateSquareArea and CalculateRectangleArea. This is because compiler does not know where to find these functions as we are not compiling the other two functions before the main function.
> go run .\main.go
# command-line-arguments
.\main.go:13:16: undefined: CalculateSquareArea
.\main.go:17:19: undefined: CalculateRectangleArea

 

NOTE:

Please note that I am executing all the commands in the Window’s PowerShell.

 

[Solved] How to Fix the Error .\main.go: undefined in Golang

Also Read: How to Implement Queue in Golang [Step by Step Guide]

So far we have gained the understanding on why the “.\main.go: undefined” error occurred and how to fix it. Let us now look at the different ways to fix the error. In this section, we will discuss about two possible ways to solve the error. Let us look at each solution one by one as given below.

 

Option-1: Run all the .go Files

The first way to address the error is that we will compile all the .go file in the current package. This way, main.go file will be able to locate the reference to the functions that are defined in the different files of the same package. There are further two ways to execute all the .go files.

1.  Explicitly mention all .go file as command argument

You can provide each .go file name in the  go run command as shown below. It there are no errors in the code, the response will return the expected output. This method is suitable when we have only couple of .go files in the same package. What if the number of .go files are large? say 10-20 .go files. In that case, this command is not feasible.

> go run .\square.go .\rectangle.go .\main.go
Area of Square with side 5.00: 25.00
Area of Rectangle with length 4.00 and width 6.00: 24.00

 

2. Use .(Dot) Operator  

To overcome the challenge of above command, we can instead use "." (Dot) operator as command argument to execute all the .go files in the current package. To do so, execute below command. The response will return the same output as it was returned earlier.

> go run .
Area of Square with side 5.00: 25.00
Area of Rectangle with length 4.00 and width 6.00: 24.00

 

Option-2: Build a Binary

Another way to resolve the error is by creating a go binary. We will execute the command go build <file-name> to create a binary in the current package. To do so, execute below command.  This will generate a binary at the package level where main.go file is defined.

> go build .

 

Above command will generate a binary at package level as shown below.

 

Next, once the binary is executed, you can easily execute the binary now to run the application as shown below. This solution is suitable for the production use case whereas for development use case, you can opt for Option-1 or Options-2 which ever suits your requirement.

> .\area.exe
Area of Square with side 5.00: 25.00
Area of Rectangle with length 4.00 and width 6.00: 24.00

 

Summary

We have successfully fixed the error using one of the solution given in this tutorial. We also reproduced the error before going through the solutions to make sure we are addressing the right issue.

Leave a Comment