Newbie - import function library

Hi

I’m writing my first serious Go application and running into what must be a very silly issue.

I have a file: “engine.go” which is package main and does 80% of what I need. But it is a little disorganized and littered with functions and would benefit from being split into different files.

The program is arranged like this:

rootfolder
    engine
        engine.go
        functionlib.go
    queries
        stuff.json
        morestuff.json
    results
        results.csv

Where engine, queries and results are all subfolders under the root folder.

In my main.go, how do I import the functions in the functionlib.go file?
Does the functionlib.go file need a package name?

Would appreciate a steer. Thanks!

What are the packages you use in engine.go and functionlib.go? Is it engine?

If yes, you don’t have to import anything. Both files are part of the same package and know each other.

Hi,

Thanks.

Initially the engine.go code was package main and the functionlib was in package main.

I have tried changing the package in both files to “engine”, but now when trying to execute the engine.go all I get is:

>go run engine.go
go run: cannot run non-main package

I need to revisit/learn more about code organisation.

Is there anything I can do just now just to get the functionlib “referenceable” and usable?

Thanks

If you want have an executable, the files must be in package main. This should solve your problem.

Thanks.
To be honest I’m getting nowhere fast though. I’ve stripped things right back to a super simple example of what I want to do then once I understand that properly I will revisit the func library in my main project.

Here is the scenario for the super simple example.

I have, in a folder named “gotest”, the following files:-

test.go and libs.go

test.go is:

package main

import (
	"fmt"
)

func main() {
	fmt.Println("This is a message from main.go")
	SaySomething()
}

libs.go is:

package main

import (
	"fmt"
)

// FATAL: Just a token fatal message
func SaySomething() {

	fmt.Println("This is from the lib!!")
}

Yet, when I try to run test.go all I get is:
>go run test.go
# command-line-arguments
.\test.go:11: undefined: SaySomething
>

Forgive me, but I had thought that if both files were in the main package then this should work okay without the need to import anything else?

What am I missing?

Thanks.

Of course… 12 seconds after posting the answer hits me…

I was running JUST test.go

when I do:

go run test.go libs.go
This is a message from main.go
This is from the lib!!

Then its all happy and shiny again.

When I build the package (reffing both files) its all good too.

Thanks and sorry for wasting your time.

But thats not how you should do it.

The proper way where to have it like this:

$GOPATH/src/github.com/user/project/cmd/main.go:

package main

import (
	"fmt"

	"github.com/user/project/say"
)

func main() {
	fmt.Println("This is a message from main.go")
	say.Something()
}

$GOPATH/src/github.com/user/project/say/something.go:

package say

import (
	"fmt"
)

// FATAL: Just a token fatal message
func Something() {
	fmt.Println("This is from the lib!!")
}

I had a good read about how to organize go-projects a while ago, and I think it was this article:

https://medium.com/@benbjohnson/structuring-applications-in-go-3b04be4ff091

I have to be honest though, I’m not quite sure if it really was that one…

1 Like

Thanks @NobbZ

Much appreciated. Till now I have avoided placing any of my Go code into the src directory. It sounds like I just need to get over it and start using that path for my projects.

Thanks again.

Where have you put it instead?

Just staying in one or multiple workspaces made life so much easier for me.

My application is going to be called “guess” and will need to work with several different types of data including json, connection strings, results paths and other items.

So I made a folder called guess in the root of C: and it looks like this:

C:\
->guess
    |
    ----> engine <folder>
      |    ------> engine.go
      |    ------> funcslib.go
    ----> queries <folder>
    ----> results <folder>
    ----> paths <folder>
    ----> instructions <folder>

The folders are filled with datasources that the tool needs to run properly. Its basically an industrial sized query poster which collects lots of responses from loads of different services and endpoints and collates all the results according to how the user has configured their instructions.json in the instructions folder.

I wanted to run engine.go and call functions that are located in the funcslib.go file. And that does work just fine since my “'D’oh” moment about how I was calling ‘go run’ and ‘go build’. Since learning that I have to reference both files, it works just great.

Am I doing it even wronger than I thunk?

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