Skip to content

Commit

Permalink
Merge pull request #763 from devlights/add-os-setenv-example
Browse files Browse the repository at this point in the history
  • Loading branch information
devlights authored Feb 28, 2024
2 parents a201d04 + 1cf488d commit f3a6dd8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/basic/osop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
| lookupenv.go | osop_lookupenv | os.LookupEnv() のサンプルです。 |
| expandenv.go | osop_expandenv | os.ExpandEnv() のサンプルです。 |
| expand.go | osop_expand | os.Expand() のサンプルです。 |
| setenv.go | osop_setenv | os.Setenv() のサンプルです。 |
1 change: 1 addition & 0 deletions examples/basic/osop/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
m["osop_lookupenv"] = LookupEnv
m["osop_expandenv"] = ExpandEnv
m["osop_expand"] = Expand
m["osop_setenv"] = Setenv
}
51 changes: 51 additions & 0 deletions examples/basic/osop/setenv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package osop

import (
"os"

"github.com/devlights/gomy/output"
)

// Setenv は、os.Setenv() のサンプルです。
//
// 既に存在する環境変数に対して os.Setenv() した場合は
// そのプロセス内で値が上書きされる。
//
// # REFERENCES
//
// - https://pkg.go.dev/[email protected]#Setenv
func Setenv() error {
var err error

err = os.Setenv("MYENV1", "HELLOWORLD")
if err != nil {
return err
}

output.Stdoutl("[MYENV1]", os.Getenv("MYENV1"))

err = os.Setenv("HOSTNAME", "HELLOWORLD")
if err != nil {
return err
}

output.Stdoutl("[HOSTNAME]", os.Getenv("HOSTNAME"))

return nil

/*
$ task
task: [build] go build .
task: [run] ./try-golang -onetime
ENTER EXAMPLE NAME: osop_setenv
[Name] "osop_setenv"
[MYENV1] HELLOWORLD
[HOSTNAME] HELLOWORLD
[Elapsed] 61.65µs
*/

}

0 comments on commit f3a6dd8

Please sign in to comment.