Use function in another Golang folder

Asked

Viewed 218 times

1

I created a pacote inside $GOPATH/src/github.com/meu-user/meupacote with a file meupacote.go who has the function main and another folder with the name productsthat holds the file products.go, in this my file products.go have a function getBySKU, how can I use this function in my main file meupacote.go? See the example below:

- meupacote
  - products
    -products.go
  - meupacote.go

I am trying to use it directly because it is "within the same package" and I only get the error that the function getBySKU is undefined, I also tried to define the function as "exportable" with GetBySKU and the same error remains.

1 answer

1


Within the structure that you put, you can define products as a package

products go.

package products
(...)

And in my meupacote.go you do import by the usual way, but setting meupacote as the root of products!

my pad.go

(...)
import "github.com/meu-user/meupacote/products"

Remembering to define the function GetBySKU as exportable

  • 1

    Your reply helped me to reach the solution! thanks! but as I am creating in my $GOPATH to import it I had to put `import "github.com/my-user/my-package/product"

  • True! I forgot that detail. I have already accepted the edition suggested by Mario Idival that corrects this

Browser other questions tagged

You are not signed in. Login or sign up in order to post.