-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosexample.go
53 lines (46 loc) · 1.1 KB
/
osexample.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"os"
)
func main() {
// Example 1: Get environment variables
fmt.Println("Environment Variables:")
for _, env := range os.Environ() {
fmt.Println(env)
}
// Example 2: Get the current working directory
cwd, err := os.Getwd()
if err != nil {
fmt.Println("Error getting current directory:", err)
} else {
fmt.Println("Current Working Directory:", cwd)
}
// Example 3: Create and remove a file
fileName := "example.txt"
file, err := os.Create(fileName)
if err != nil {
fmt.Println("Error creating file:", err)
} else {
fmt.Println("File created:", fileName)
file.Close()
}
err = os.Remove(fileName)
if err != nil {
fmt.Println("Error removing file:", err)
} else {
fmt.Println("File removed:", fileName)
}
// Example 4: Read command-line arguments
fmt.Println("Command-line Arguments:")
for i, arg := range os.Args {
fmt.Printf("Arg %d: %s\n", i, arg)
}
// Example 5: Get the hostname of the machine
hostname, err := os.Hostname()
if err != nil {
fmt.Println("Error getting hostname:", err)
} else {
fmt.Println("Hostname:", hostname)
}
}