{"id":1441,"date":"2020-02-23T06:40:55","date_gmt":"2020-02-23T06:40:55","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=1441"},"modified":"2020-02-23T06:41:06","modified_gmt":"2020-02-23T06:41:06","slug":"check-environment-variable-set-go","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/check-environment-variable-set-go\/","title":{"rendered":"Check if a environment variable is set in Go (Golang)"},"content":{"rendered":"\n
os.LookupEnv<\/strong> function can be used to check whether a particular environment variable is set or not. It returns a bool which is true if the given env variable set otherwise false. Let’s see a working code:<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n os.LookupEnv function can be used to check whether a particular environment variable is set or not. It returns a bool which is true if the given env variable set otherwise false. Let’s…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[200],"class_list":["post-1441","post","type-post","status-publish","format-standard","hentry","category-tech","tag-env"],"yoast_head":"\npackage main\n\nimport (\n \"fmt\"\n \"log\"\n \"os\"\n)\n\nfunc main() {\n \/\/Set env a to b\n err := os.Setenv(\"a\", \"x\")\n if err != nil {\n log.Fatal(err)\n }\n\n val, present := os.LookupEnv(\"a\")\n fmt.Printf(\"a env variable present: %t\\n\", present)\n fmt.Println(val)\n\n val, present = os.LookupEnv(\"b\")\n fmt.Printf(\"b env variable present: %t\\n\", present)\n}<\/code><\/pre>\n\n\n\n
a env variable present: true\nx\nb env variable present: false<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"