-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #764 from devlights/add-os-unserenv-example
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package osop | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/devlights/gomy/output" | ||
) | ||
|
||
// Unsetenv は、os.Unsetenv() のサンプルです。 | ||
// | ||
// 指定された環境変数の値をクリアします。 | ||
// 一時的な環境変数を用意する際に、os.Setenv()とペアで以下のように | ||
// よく利用される。 | ||
// | ||
// os.Setenv("MYENV", "HELLOWORLD") | ||
// defer os.Unsetenv("MYENV") | ||
// | ||
// # REFERENCES | ||
// | ||
// - https://pkg.go.dev/[email protected]#Unsetenv | ||
func Unsetenv() error { | ||
const ( | ||
ENVKEY = "MYENV" | ||
ENVVAL = "HELLOWORLD" | ||
) | ||
|
||
var ( | ||
env string | ||
ok bool | ||
err error | ||
) | ||
|
||
err = os.Setenv(ENVKEY, ENVVAL) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
env, ok = os.LookupEnv(ENVKEY) | ||
output.Stdoutf("[MYENV(before)]", "VALUE=%q\tOK=%v\n", env, ok) | ||
|
||
err = os.Unsetenv(ENVKEY) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
env, ok = os.LookupEnv(ENVKEY) | ||
output.Stdoutf("[MYENV(after )]", "VALUE=%q\tOK=%v\n", env, ok) | ||
|
||
return nil | ||
|
||
/* | ||
$ task | ||
task: [build] go build . | ||
task: [run] ./try-golang -onetime | ||
ENTER EXAMPLE NAME: osop_unsetenv | ||
[Name] "osop_unsetenv" | ||
[MYENV(before)] VALUE="HELLOWORLD" OK=true | ||
[MYENV(after )] VALUE="" OK=false | ||
[Elapsed] 70.13µs | ||
*/ | ||
|
||
} |