Cannot find package [SOLVED]

Hi! New with Go!
My structure is:

E:
…GoWorkSpace
…src
…dp_project
main.go
controller.go
…pkg
…bin

The GOPATH is defined at: E:\GoWorkspace, and the projects are created at SRC like src\myproject; the go’s installation is C:\GO and works
I have two files, same package - main package - main.go and controller.go but when I refer to one function that resides in controller, the main break and… :frowning:
Can anyone help-me plz.
Excuse the english…

main.go -----------------------------
package main

import (
“teste”
)

func main() {
r := registerRoutes()
r.Run(":3000")
}

controller.go

package teste

import (
“net/http”

"github.com/gin-gonic/gin"

)

func registerRoutes() *gin.Engine {

r := gin.Default()
r.LoadHTMLGlob("./templates/**/*.html") // O DUPLO ASTERISCO INDICA ENTRAR EM TODOS DIRETÓRIOS
r.GET("/", func(c *gin.Context) {
	c.HTML(http.StatusOK, "index.html", nil)
})

Thanks in advance

What command do you execute? What does it say?

What is “teste”?

Thank you for your attention,
Please disregard TESTE, in place use MAIN, is the correct code for controller archive.
Although the two files have the same PACKAGE (MAIN PACKAGE) , the system throw an error when from main.go the registerRoutes() function is called.
This problem is related, i think, with my structure.
Thanks in advance.

This error:

command-line-arguments

.\main.go:6: undefined: registerRoutes

The general rule is one package per directory. The go tools expand import paths to %GOPATH%\src\<import path> and expect all the buildable files in that directory to be in the same package.

In the code you posted, main.go is in package main while controller.go is in package teste. They should both have package main.

When files in the same directory have the same package, you do not need to import anything to get access to the functions in the other files. Use files to organize code in the same package.

go build works with the following two files in the same directory (this is the same as your structure):

main.go

package main

func main() {
	r := registerRoutes()
	r.Run(":3000")
}

controller.go

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func registerRoutes() *gin.Engine {
	r := gin.Default()
	r.LoadHTMLGlob("./templates/**/*.html") // O DUPLO ASTERISCO INDICA ENTRAR EM TODOS DIRETÓRIOS
	r.GET("/", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.html", nil)
	})

	//...
	return r
}

Thank you Nathankerr!
But I make a mistake, both archives are in the same packages.
I made an experience, new code, easy and this is bad response:
#command-line-arguments
.\main.go:8 undefined: rregisterRoutes

I think that this error is related my application structure.
The code bellow. My structure is described at first post in this THREAD.
Thanks in advance.


main.go
package main

import (
“fmt”
)

func main() {
fmt.Println(rregisterRoutes())
}

controller.go
package main

func rregisterRoutes() string {
return “Hello World”
}

Are you by any chance running the command
go run main.go
?
go run only compiles the file you gave it and not the whole folder, so it can’t find registerRoutes()
You need to run go build or go install and then call the executable (in your case dp_project.exe)
You could also run go run main.go controller.go but that quickly gets unwieldy as you get more files, so a common pattern is to call go build && myprogram.exe

Hope that helps :slight_smile:

2 Likes

Thank you Julien!
The program compiles and runs perfectly.
I will read more, much more about GOLANG. I need this.
By the way, thank you all

ps: Excuse the english.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.