-2

I have some Go code I want to test locally, then deploy to AWS. Everything is fine as long as I stay in a single directory/package.

However, I want my folder structure to be similar to this:

$GOPATH/src/project/application.go

$GOPATH/src/project/lib1/libcode.go

Locally, to use the code in "lib1" I need to use import "project/lib1"

But when deployed to AWS the "project" folder doesn't exist of course, so instead I have to use import "./lib1"

I can probably solve this problem by either having all code in a single folder/package, or by changing my $GOROOT every time I change working on a project, but both these workarounds feel dirty and awkward.

What's the correct way to tackle this? Thanks.

Herco
  • 377
  • 2
  • 9
  • 1
    "_But when deployed to AWS the "project" folder doesn't exist of course_" Could you clarify what you mean by that? Also, how do you "_deploy_" your project on AWS? – Giulio Micheloni Jul 16 '19 at 10:45
  • have you considered using go modules? It may solve the problem for you. Is the project open-source? If no, you'll need to give the AWS ec2 access to the repo's dependencies. More about go modules -> https://blog.golang.org/using-go-modules – kabanek Jul 16 '19 at 10:50
  • @GiulioMicheloni - I deploy to Elastic Beanstalk using the EB CLI (eb deploy). About the missing folder: locally I need to provide the full path from my GOPATH upwards, but these folders don't exist anywhere else but on my computer. – Herco Jul 16 '19 at 11:30
  • @kabanek I will read up on modules, thank you. – Herco Jul 16 '19 at 11:32
  • 1
    @Herco, are you deploying only the `application.go`? You need to deploy the entire folder of your project, i.e. also the dependency packages. According to AWS EB doc: "_If git is installed, EB CLI uses the git archive command to create a .zip file from the contents of the most recent git commit command._". So, try to create a git repo in your project folder and then deploy again. If all your dependencies are in the project folder (maybe under a `vendor` folder), then you should be able to build it also inside the AWS VM. – Giulio Micheloni Jul 16 '19 at 12:24
  • @GiulioMicheloni Yes, I have a repo, I am committing and pushing with Git first, then `eb deploy` deploys/copies the entire branch. But once I create a package in a subfolder of the project, the import commands differ on local v.s. deployed since locally I need to specify the path including the project folder itself (which does not exist in Beanstalk) – Herco Jul 16 '19 at 13:30
  • The normal deployment mechanism for Go is to build the project, which produces a self-contained binary, and to deploy that binary - *not* the source code. A production machine running a Go application does not need the Go source files and does not need the Go toolchain installed. – Adrian Jul 16 '19 at 13:32
  • 1
    It looks like you're using relative import paths. Don't do that! https://stackoverflow.com/questions/38517593/relative-imports-in-go – Jonathan Hall Jul 16 '19 at 13:47
  • @Adrian Thanks, I've got it working. It feels a bit weird though, coming from other languages/stacks. If you write it as an answer I'll verify. – Herco Jul 16 '19 at 14:42

1 Answers1

0

The normal deployment mechanism for Go is to build the project, which produces a self-contained binary, and to deploy that binary - not the source code. A production machine running a Go application does not need the Go source files and does not need the Go toolchain installed. Go is not an interpreted language, it's not even a bytecode language with a VM (like Java with JVM or C# with CLR); it produces native binaries for the target OS and architecture. That's one of the reasons for its strong performance and efficient memory profile.

Adrian
  • 42,911
  • 6
  • 107
  • 99