Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building with multiple files in GO (GO1)

Tags:

go

build

Does anyone know if it's possible to use the "go build" command to build from multiple files in a directory? For example

src/
  file1.go
  file2.go

Where file1.go contains the "func main()" method and file2 provides supporting functions. I've tried by using the following import statements but I'm getting no where

import (
  "file2"
)

import (
  file2 "./file2"
)

I'm wondering if this is a case that I need to modify the GOROOT environment variable to get this to work. Or if I'm just being daft and missing something blindingly obvious.

Thanks

like image 823
daz-fuller Avatar asked Dec 07 '25 21:12

daz-fuller


1 Answers

If file1.go and file2.go are part of the same package, this should work fine. You don't have to import files from the same package into each other. Their variables and functions are already shared.

If the files belong to different packages, they should be in different directories.

like image 130
Evan Shaw Avatar answered Dec 10 '25 21:12

Evan Shaw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!