Last active
August 24, 2020 16:44
-
-
Save mgencur/6097e8f298c12dc1634e2ecad709acd7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main.go: ----------------------------- | |
package main | |
import ( | |
"fmt" | |
"github.com/mgencur/awesomeProject/upgrade" | |
_ "github.com/mgencur/awesomeProject/upgrade/registerplugin" | |
) | |
func main() { | |
if upgrade.Upgradable == nil { | |
fmt.Println("is nil") | |
upgrade.Upgradable = defaultUpgrade{} | |
} | |
upgrade.Upgradable.Upgrade() | |
} | |
type defaultUpgrade struct {} | |
func (l defaultUpgrade) Upgrade() { | |
fmt.Println("Default upgrade") | |
} | |
func (l defaultUpgrade) Downgrade() { | |
fmt.Println("Default downgrade") | |
} | |
upgrade/upgradable.go: ------------------------------- | |
package upgrade | |
type upgradable interface { | |
Upgrade() | |
Downgrade() | |
} | |
var Upgradable upgradable | |
upgrade/registerplugin/register.go: ------------------------ | |
package plugin | |
// By default this will be empty, but a plugin can be registered by importing a package with that plugin here, or the user can create another file in this package with the import | |
import _ "github.com/mgencur/awesomeProject/plugin" | |
plugin/plugin.go: --------------------------------------------------------- | |
package plugin | |
import ( | |
"fmt" | |
"github.com/mgencur/awesomeProject/upgrade" | |
"testing" | |
) | |
type upgrade2 struct {} | |
func (l upgrade2) Upgrade() { | |
fmt.Println("New upgrade") | |
} | |
func (l upgrade2) Downgrade() { | |
fmt.Println("New downgrade") | |
} | |
type UpgradeSuite struct { | |
PreUpgradeTests []UpgradeTestsElement | |
} | |
type UpgradeTestsElement interface { | |
Name() string | |
Handler() func(t *testing.T) | |
} | |
func init() { | |
fmt.Println("Init done") | |
upgrade.Upgradable = upgrade2{} // here the implementation is switched to a new one | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment