Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: go build support for importing/linking additional packages #14088

Closed
CAFxX opened this Issue Jan 25, 2016 · 7 comments

Comments

Projects
None yet
5 participants
@CAFxX
Copy link
Contributor

CAFxX commented Jan 25, 2016

I would love to be able to specify during go build/run additional packages to be imported. This would be mostly useful for those packages that are imported for their side effects.

As an example consider:

$ go build example.com/myapp -import github.com/go-sql-driver/mysql

This should have the same effect as adding the following import in the root package of myapp:

import _ "github.com/go-sql-driver/mysql"

Right now I'm using a quick-and-dirty tool for this but I really can't help thinking this should be part of the go command.

This would basically enable a very rudimental form of (compiler-mediated) DI.

@CAFxX CAFxX changed the title go build support for importing additional packages go build support for importing/linking additional packages Jan 25, 2016

@minux

This comment has been minimized.

Copy link
Member

minux commented Jan 25, 2016

@CAFxX

This comment has been minimized.

Copy link
Contributor Author

CAFxX commented Jan 25, 2016

This assumes that the myapp author has either included all possible database/sql drivers, or that it provides a file for each database/sql driver with its own tag. Even just with SQL drivers (https://github.com/golang/go/wiki/SQLDrivers lists more than 20) this becomes hard quickly: imagine what could happen with application modules or plugins. Moreover, if the myapp maintainer doesn't want to import a specific package, you need to patch manually every time you build/run.

Sure, the current state is not unbearable... it just feels like an unneeded hop to jump through.

@minux

This comment has been minimized.

Copy link
Member

minux commented Jan 25, 2016

@CAFxX

This comment has been minimized.

Copy link
Contributor Author

CAFxX commented Jan 25, 2016

The fact that the use of something should be minimized doesn't imply that that something is not useful if used properly. I'm not advocating abuse of the feature I'm proposing; I'm advocating that it would be useful in certain limited scenarios - similarly to build tags, the -X flag to go tool link and cgo (all things that make reproducing an exact build basically impossible).

@ianlancetaylor ianlancetaylor changed the title go build support for importing/linking additional packages cmd/go: go build support for importing/linking additional packages Jan 25, 2016

@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Jan 25, 2016

@bcmills

This comment has been minimized.

Copy link
Member

bcmills commented Jan 23, 2019

This request is missing a key detail: when would the package initializers for the extra packages run? (Before the initializers for the packages imported from package main, or after?)

At the moment, we have the nice property that package initializers run in a strict topological order.

@bcmills

This comment has been minimized.

Copy link
Member

bcmills commented Jan 23, 2019

Note that you can structure something more-or-less as @minux suggests using modules and replace directives.

example.com/main/go.mod:

module example.com/main

require example.com/inject v1.0.0

example.com/main/main.go:

package main

import _ "example.com/inject"
[…]

example.com/inject/go.mod

module example.com/inject
[…]

example.com/inject/inject.go:

package inject

import _ "github.com/go-sql-driver/mysql"

my.localhost/go.mod:

module my.localhost

require example.com/main v1.0.0

replace example.com/inject => ./inject

my.localhost/inject/go.mod:

module example.com/inject
[…]

my.localhost/inject/inject.go:

package inject

import _ "my.localhost/sql-driver"
@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Feb 23, 2019

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@gopherbot gopherbot closed this Feb 23, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.