Go Imports for Multi-file go programs


I keep all my code in a single go file and it starts to getting big so I wanted to separate into multiple go files. I currently have it setup and working fine but I have to reuse a lot of imports from my main.go file in “other” go files. These “other” go files are separated into different folders under different package names, is this inefficient by any chance in that I am reloading same imports twice? Is this the correct way to setup a multi-file go program?

For example:

under root folder:

package main

import (

func main(){
     fmt.Println("blah blah")

under root/gostuff

package gostuff
    import (
          "fmt"   <- duplicate import a problem or no? Is this the correct way to do this?
    func CallFunc(){
           fmt.Println("random text blah")

I suggest going through https://golang.org/doc/code.html and maybe https://golang.org/doc/effective_go.html for some tips and insights. In general, splitting your stuff into multiple files is good, repeating imports in every file is necessary and harmless, and you should probably not split into directories as much as you might be doing. You should not use relative imports ("./gostuff").


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