Skip to content

Commit f3a6dd8

Browse files
authored
Merge pull request #763 from devlights/add-os-setenv-example
2 parents a201d04 + 1cf488d commit f3a6dd8

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

examples/basic/osop/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
| lookupenv.go | osop_lookupenv | os.LookupEnv() のサンプルです。 |
1212
| expandenv.go | osop_expandenv | os.ExpandEnv() のサンプルです。 |
1313
| expand.go | osop_expand | os.Expand() のサンプルです。 |
14+
| setenv.go | osop_setenv | os.Setenv() のサンプルです。 |

examples/basic/osop/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
2020
m["osop_lookupenv"] = LookupEnv
2121
m["osop_expandenv"] = ExpandEnv
2222
m["osop_expand"] = Expand
23+
m["osop_setenv"] = Setenv
2324
}

examples/basic/osop/setenv.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package osop
2+
3+
import (
4+
"os"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// Setenv は、os.Setenv() のサンプルです。
10+
//
11+
// 既に存在する環境変数に対して os.Setenv() した場合は
12+
// そのプロセス内で値が上書きされる。
13+
//
14+
// # REFERENCES
15+
//
16+
// - https://pkg.go.dev/os@go1.22.0#Setenv
17+
func Setenv() error {
18+
var err error
19+
20+
err = os.Setenv("MYENV1", "HELLOWORLD")
21+
if err != nil {
22+
return err
23+
}
24+
25+
output.Stdoutl("[MYENV1]", os.Getenv("MYENV1"))
26+
27+
err = os.Setenv("HOSTNAME", "HELLOWORLD")
28+
if err != nil {
29+
return err
30+
}
31+
32+
output.Stdoutl("[HOSTNAME]", os.Getenv("HOSTNAME"))
33+
34+
return nil
35+
36+
/*
37+
$ task
38+
task: [build] go build .
39+
task: [run] ./try-golang -onetime
40+
41+
ENTER EXAMPLE NAME: osop_setenv
42+
43+
[Name] "osop_setenv"
44+
[MYENV1] HELLOWORLD
45+
[HOSTNAME] HELLOWORLD
46+
47+
48+
[Elapsed] 61.65µs
49+
*/
50+
51+
}

0 commit comments

Comments
 (0)