Skip to content

Commit a201d04

Browse files
authored
Merge pull request #762 from devlights/add-os-expand-example
2 parents 8998e80 + 71cf864 commit a201d04

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

examples/basic/osop/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
| getenv.go | osop_getenv | os.GetEnv() のサンプルです。 |
1111
| lookupenv.go | osop_lookupenv | os.LookupEnv() のサンプルです。 |
1212
| expandenv.go | osop_expandenv | os.ExpandEnv() のサンプルです。 |
13+
| expand.go | osop_expand | os.Expand() のサンプルです。 |

examples/basic/osop/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
1919
m["osop_getenv"] = GetEnv
2020
m["osop_lookupenv"] = LookupEnv
2121
m["osop_expandenv"] = ExpandEnv
22+
m["osop_expand"] = Expand
2223
}

examples/basic/osop/expand.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package osop
2+
3+
import (
4+
"os"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// Expand は、os.Expand() のサンプルです。
10+
//
11+
// Expandは、マッピング関数に基づいて文字列の${var}または$varを置き換えます。
12+
// マッピング関数の書式は
13+
//
14+
// func(string) string
15+
//
16+
// となっています。
17+
// os.ExpandEnv() は、以下と同じことになります。
18+
//
19+
// os.Expand(s, os.Getenv)
20+
//
21+
// # REFERENCES
22+
//
23+
// - https://pkg.go.dev/os@go1.22.0#Expand
24+
func Expand() error {
25+
var (
26+
fn = func(s string) string {
27+
return "helloworld"
28+
}
29+
v = os.Expand("${HI}", fn)
30+
)
31+
32+
output.Stdoutl("[HI]", v)
33+
output.Stdoutl("[HOME]", os.Expand("${HOME}", os.Getenv))
34+
35+
return nil
36+
37+
/*
38+
$ task
39+
task: [build] go build .
40+
task: [run] ./try-golang -onetime
41+
42+
ENTER EXAMPLE NAME: osop_expand
43+
44+
[Name] "osop_expand"
45+
[HI] helloworld
46+
[HOME] /home/gitpod
47+
48+
49+
[Elapsed] 11.04µs
50+
*/
51+
52+
}

examples/basic/osop/expandenv.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,21 @@ func ExpandEnv() error {
2828
output.Stdoutf("[env3]", "%q\n", env3)
2929

3030
return nil
31+
32+
/*
33+
$ task
34+
task: [build] go build .
35+
task: [run] ./try-golang -onetime
36+
37+
ENTER EXAMPLE NAME: osop_expandenv
38+
39+
[Name] "osop_expandenv"
40+
[env1] "/home/gitpod"
41+
[env2] ""
42+
[env3] "home is /home/gitpod, hostname is devlights-trygolang-96ahwfwl0rp"
43+
44+
45+
[Elapsed] 33.6µs
46+
*/
47+
3148
}

0 commit comments

Comments
 (0)