Skip to content

Commit

Permalink
Merge pull request #758 from devlights/add-os-env-example
Browse files Browse the repository at this point in the history
  • Loading branch information
devlights authored Feb 26, 2024
2 parents f7b81a3 + 504e05f commit 60cf608
Show file tree
Hide file tree
Showing 4 changed files with 76 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 @@ -7,3 +7,4 @@
| mkdir.go | osop_mkdir | os.Mkdir/MkdirAllのサンプルです. |
| listprocess.go | osop_list_processes | プロセスリストを取得するサンプルです. |
| environ.go | osop_environ | os.Environ()のサンプルです。 |
| getenv.go | osop_getenv | os.GetEnv() のサンプルです。 |
18 changes: 18 additions & 0 deletions examples/basic/osop/environ.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
// 戻り値は []string 。
//
// # REFERENCES
//
// - https://pkg.go.dev/os@go1.22.0#Environ
func Environ() error {
for _, env := range os.Environ() {
Expand All @@ -30,4 +31,21 @@ func Environ() error {
}

return nil

/*
$ task
task: [build] go build .
task: [run] ./try-golang -onetime
ENTER EXAMPLE NAME: osop_environ
[Name] "osop_environ"
[env] HISTFILE /workspace/.gitpod/.shell_history
[env] HOME /home/gitpod
[env] HOSTNAME devlights-trygolang-q7kq6quld1n
[Elapsed] 29.6µs
*/

}
1 change: 1 addition & 0 deletions examples/basic/osop/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
m["osop_mkdir"] = Mkdir
m["osop_list_processes"] = ListProcesses
m["osop_environ"] = Environ
m["osop_getenv"] = GetEnv
}
56 changes: 56 additions & 0 deletions examples/basic/osop/getenv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package osop

import (
"os"

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

// GetEnv は、os.GetEnv() のサンプルです。
//
// Getenvは、キーで指定された環境変数の値を取得します。
// 値が返されるが、変数が存在しない場合は空が返ります。
// 空の値と未設定の値を区別するには、LookupEnvを使用します。
//
// 戻り値は string で、error は返却されない。
//
// # REFERENCES
//
// - https://pkg.go.dev/os@go1.22.0#Getenv
func GetEnv() error {
const (
ENV1 = "HOSTNAME"
ENV2 = "SONZAISHINAIKEY"
)

var (
env1 = os.Getenv(ENV1)
env2 = os.Getenv(ENV2)
)

//
// env2 の方は存在しない環境変数のため空が返る。
// この「空」の値が、「存在しない環境変数」なのか「存在するが値が空」なのかを
// 見極める必要がある場合は、os.LookupEnv() の方を使う。
//
output.Stdoutl("[env1]", env1)
output.Stdoutl("[env2]", env2)

return nil

/*
$ task
task: [build] go build .
task: [run] ./try-golang -onetime
ENTER EXAMPLE NAME: osop_getenv
[Name] "osop_getenv"
[env1] devlights-trygolang-q7kq6quld1n
[env2]
[Elapsed] 43.03µs
*/

}

0 comments on commit 60cf608

Please sign in to comment.