Import package on demand in Go?

go, import

Solution

no. Golang is a static typed language. Everything has to be defined at compile time.

You can activate / deactivate the profiling with a flag though.

or use a build trick

// +build profile
package "mypackage"

import ( 
  _ "profiling" 
)

and then build with

go build -tags=profile  

Problem

Is there a way to import a package on demand? Use case is the import of a profiling module, which I only want to import, when a certain command line flag has been set.

Original source

Related problems