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
Closed

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

CAFxX opened this issue Jan 25, 2016 · 7 comments
Labels
FeatureRequest FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@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
Copy link
Member

minux commented Jan 25, 2016 via email

@CAFxX
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
Copy link
Member

minux commented Jan 25, 2016 via email

@CAFxX
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 bcmills added FeatureRequest NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Jan 23, 2019
@bcmills
Copy link
Contributor

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
Copy link
Contributor

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"

@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jan 23, 2019
@gopherbot
Copy link

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.)

@golang golang locked and limited conversation to collaborators Feb 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants