Home > Blockchain >  Run a single Go file with a single local import without using modules
Run a single Go file with a single local import without using modules

Time:10-31

I have a series of go files that are linked by use but are logically independent. They all use a common set of helper functions defined in a separate file.

My directory structure is shown below.

src/
├── foo1.go
├── foo2.go
├── ...
├── fooN.go
└── helper/
      └── helper.go

The foox.go files are all of this form -

package main

import help "./helper"

// functions and structs that use functionality in
// helper but are not related to anything going on
// in other foox.go files

func main() {
    // more things that uses functionality in helper
    // but are not related to anything going on in
    // other foox.go files

    return
}

I was running specific files with go run foox.go, but recently updated my version of Go. This workflow is now broken since relative imports are no longer permitted -

"./helper" is relative, but relative import paths are not supported in module mode

What is the correct the way to structure a collection independent Go files that all depend on the same collection of helper functions?

All the guidance says to use modules, but in this case that will mean having a separate module for each foox.go file where each file contains funcs, structs etc. that will never be used in any other module.

All I want to do is be able run a single .go file that includes another single local .go file without going through the hassle of making dozens of modules.

CodePudding user response:

You can disable module mode by setting the environment variable GO111MODULE=off

  • Related