aboutsummaryrefslogtreecommitdiff
path: root/business/business.go
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2024-12-19 15:16:56 +0000
committerdakkar <dakkar@thenautilus.net>2024-12-19 15:18:37 +0000
commitcafc27302d7fdf69934f3b5007ddf3f5e0fc1cd4 (patch)
tree6164fa82fb8f52b6eb47d06e36f9f97442e4569d /business/business.go
parentmove more code in the factory (diff)
downloadgo-example-master.tar.gz
go-example-master.tar.bz2
go-example-master.zip
add another package to better show the patternHEADmaster
Diffstat (limited to 'business/business.go')
-rw-r--r--business/business.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/business/business.go b/business/business.go
new file mode 100644
index 0000000..ecb54ad
--- /dev/null
+++ b/business/business.go
@@ -0,0 +1,45 @@
+package business
+
+import (
+ "fmt"
+
+ "www.thenautilus.net/cgit/go-example/config"
+ "www.thenautilus.net/cgit/go-example/something"
+)
+
+type OverError struct {
+ Value, Threshold int
+}
+
+func (e OverError) Error() string {
+ return fmt.Sprintf("value %d is above threshold %d", e.Value, e.Threshold)
+}
+
+type Logic struct {
+ somethingClient *something.Something
+ threshold int
+}
+
+func New(conf *config.BusinessLogicConfig, client *something.Something) Logic {
+ return Logic{
+ somethingClient: client,
+ threshold: conf.Threshold,
+ }
+}
+
+func (bl *Logic) DoSomething() error {
+ value, err := bl.somethingClient.FetchValue()
+ if err != nil {
+ return fmt.Errorf("failed to fetch value: %w", err)
+ }
+
+ if value > bl.threshold {
+ fmt.Printf("OVER! %d > %d\n", value, bl.threshold)
+
+ return OverError{value, bl.threshold}
+ }
+
+ fmt.Printf("all good")
+
+ return nil
+}