2

Example Scenario

I have an AWS S3 bucket with lots of object which a couple of my application needs to access. The applications use some info available to them to form the S3 object name, get the object and run a transformation on the object data before using it for further processing specific to the application.

I would like to create a module which will hold the logic for forming the object name, obtain it from S3 and then run the transformation on the object data so that I wont be duplicating these functions in multiple places.

In this scenario should I add AWS SDK as a dependency in the module? Keep in mind that the applications might have to use AWS SDK for something else specific to that application or they might not require it at all.

In general what is the best way to solve problems like this i.e where to add the dependency? And how to manage different versions?

Josnidhin
  • 12,469
  • 9
  • 42
  • 61
  • 5
    If you have to use AWS SDK from the module, then yes, add it as a dependency to the module. If not, then obviously don't. The Go tool handles [version selection](https://github.com/golang/go/wiki/Modules#version-selection) if multiple modules use the same dependency. – icza May 03 '19 at 14:47
  • 1
    If all the module does is deal with object keys (i.e. strings), there is no reason that the AWS SDK would be a dependency of that module (it would never be imported). Generally speaking, your dependencies are your imports. Whatever you import in your module, your module depends on; whatever you don't import, you don't depend on. The toolchain will manage that automatically. – Adrian May 03 '19 at 14:54

2 Answers2

2

If your code has to access packages from the AWS SDK then yes, you have no choice but to add it as dependency. If it doesn't and the logic is generic, or you can abstract it away from the AWS SDK then you don't need the dependency (and in fact the go tooling like go mod tidy will remove the dependency from go.mod if you add it)

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
1

In this scenario should I add AWS SDK as a dependency in the module? Keep in mind that the applications might have to use AWS SDK for something else specific to that application or they might not require it at all.

Yes, if any package from your module depends on AWS SDK, Go Modules system is going to add AWS SDK as a dependency for your module. There is nothing special you are supposed to do with your module.

Try this script with Go 1.11 or higher (and make sure to work out of GOPATH):

  1. Write your module like this:

Tree:

moduledir/packagedir1
moduledir/packagedir2
  1. Initialize the module:

Recipe:

cd moduledir
go mod init moduledir ;# or go mod init github.com/user/moduledir
  1. Build module packages:

Recipe:

go install ./packagedir1
go install ./packagedir2

Module things are supposed to automagically work!

In general what is the best way to solve problems like this i.e where to add the dependency? And how to manage different versions?

The Modules system is going to automatically manage dependencies for your module and record them in the files go.mod and go.sum. If you need to override some dependency, you should use the 'go get' command for that. For instance, see this question: How to point Go module dependency in go.mod to a latest commit in a repo?

You can also find much information on Modules here: https://github.com/golang/go/wiki/Modules

Everton
  • 12,589
  • 9
  • 47
  • 59