From d6cd31b3cd88dc57a72bb9f14e3a6b2b86d48fcf Mon Sep 17 00:00:00 2001 From: Kyle Morris Date: Tue, 6 Sep 2022 20:16:54 +0000 Subject: [PATCH 1/3] golang notes --- .github/pull_request_template.md | 1 - golang_language_guidelines.md | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) delete mode 100644 .github/pull_request_template.md create mode 100644 golang_language_guidelines.md diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md deleted file mode 100644 index 2fadc4c..0000000 --- a/.github/pull_request_template.md +++ /dev/null @@ -1 +0,0 @@ -dis work? diff --git a/golang_language_guidelines.md b/golang_language_guidelines.md new file mode 100644 index 0000000..30cfa92 --- /dev/null +++ b/golang_language_guidelines.md @@ -0,0 +1,17 @@ +# Dev vs Prod Flags +In a go package we use the init() function to define environment variables +your init() function should handle 3 cases as follows +func init() + if os.Getenv("ENV) == "prod" { + // set environment variables via either secret manager or hard coded here + os.Setenv("some var", "some value)... + thirdparty.secretmanager.getVar("some secret var", "some secret value") + + // All env vars should be accessible via os.getenv("var name") + } else if os.Getenv("ENV") == "dev" { + // same deal as above, except use dev values for staging databases etc + } else { + // enforce ENV is set and valid or else quit + panic("ENV must be set to prod or dev") + } +} From f66217d0a1c509ba3c3d2e585ecdc01c7eb84222 Mon Sep 17 00:00:00 2001 From: Kyle Morris Date: Tue, 6 Sep 2022 13:22:42 -0700 Subject: [PATCH 2/3] Update golang_language_guidelines.md --- golang_language_guidelines.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/golang_language_guidelines.md b/golang_language_guidelines.md index 30cfa92..cd4c9ca 100644 --- a/golang_language_guidelines.md +++ b/golang_language_guidelines.md @@ -1,6 +1,10 @@ # Dev vs Prod Flags In a go package we use the init() function to define environment variables + +This makes it easy to switch between dev and prod, keep track of env var changes, and works with any dev flow + your init() function should handle 3 cases as follows +``` func init() if os.Getenv("ENV) == "prod" { // set environment variables via either secret manager or hard coded here @@ -15,3 +19,6 @@ func init() panic("ENV must be set to prod or dev") } } +``` + +Then access them in your program via os.Getenv("your var...") From a7b4c775b0495cca375e872c9a2473997bfeb5d8 Mon Sep 17 00:00:00 2001 From: Kyle Morris Date: Tue, 6 Sep 2022 13:42:50 -0700 Subject: [PATCH 3/3] Update golang_language_guidelines.md --- golang_language_guidelines.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/golang_language_guidelines.md b/golang_language_guidelines.md index cd4c9ca..ccc468b 100644 --- a/golang_language_guidelines.md +++ b/golang_language_guidelines.md @@ -1,5 +1,7 @@ # Dev vs Prod Flags -In a go package we use the init() function to define environment variables +At root of go microservice define an env.go file with an init() function + +This init function looks for a single required variable "ENV" and then sets either prod or dev env vars accordingly This makes it easy to switch between dev and prod, keep track of env var changes, and works with any dev flow