Skip to content

Commit

Permalink
Added golang examples with package, exec, envs, flags
Browse files Browse the repository at this point in the history
  • Loading branch information
k8s4 committed Oct 20, 2024
1 parent f9f5ac8 commit 02f7194
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 0 deletions.
33 changes: 33 additions & 0 deletions examples/golang/cource/06a_exec/exec/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package exec

import (
"fmt"
"log"
"os/exec"
"strings"
"bytes"
)

func Run_simple(c string) string {
cmd, err := exec.Command(c).Output()
// err := cmd.Run()
if err != nil {
log.Fatal(err)
}
return string(cmd)
}

func Run_some_std_inout(s string) string {
cmd := exec.Command("tr", "a-z", "A-Z")

cmd.Stdin = strings.NewReader(s)
var out bytes.Buffer
cmd.Stdout = &out

err := cmd.Run()
if err != nil {
log.Fatal(err)
}

return fmt.Sprintf("Converted string: %q", out.String())
}
53 changes: 53 additions & 0 deletions examples/golang/cource/06a_exec/exec/pipe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package exec

import (
"fmt"
"log"
"os/exec"
"io"
"io/ioutil"
)

func Run_with_in_pipe(c string, data string) string {
cmd := exec.Command(c)
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}

go func() {
defer stdin.Close()
io.WriteString(stdin, data)
}()

out, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}

return fmt.Sprintf("%s", out)
}


func Run_with_out_pipe(c string, arg ...string) string {
cmd := exec.Command(c, arg...)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}

if err := cmd.Start(); err != nil {
log.Fatal(err)
}

data, err := ioutil.ReadAll(stdout)
if err != nil {
log.Fatal(err)
}

if err := cmd.Wait(); err != nil {
log.Fatal(err)
}

return fmt.Sprintf("%s", string(data))
}
3 changes: 3 additions & 0 deletions examples/golang/cource/06a_exec/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module 06a_exec

go 1.23.2
13 changes: 13 additions & 0 deletions examples/golang/cource/06a_exec/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"fmt"
"golang/cource/06a_exec/exec"
)

func main() {
fmt.Println(exec.Run_simple("ls"))
fmt.Println(exec.Run_some_std_inout("Test string replace"))
fmt.Println(exec.Run_with_in_pipe("cat", "Some strange test string by pipe"))
fmt.Println(exec.Run_with_out_pipe("ping", "-c 1", "8.8.8.8"))
}
18 changes: 18 additions & 0 deletions examples/golang/cource/06b_envs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// for more see to go-env library or other
package main

import (
"fmt"
"os"
)

func main() {
os.Setenv("SOME_MY_ENV", "in_the_water")
fmt.Println("SOME_MY_ENV:", os.Getenv("SOME_MY_ENV"))
fmt.Println("SEUPER_PASS:", os.Getenv("SUPER_PASS"))
fmt.Println()

for _, e := range os.Environ() {
fmt.Println(e)
}
}
24 changes: 24 additions & 0 deletions examples/golang/cource/06c_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"flag"
"fmt"
)

func main() {
nameFlag := flag.String("name", "Peter", "a string with animal name")
ageFlag := flag.Int("age", 34, "an int with age of animal")
aliveFlag := flag.Bool("alive", true, "a bool about aliveness")

var somevar string
flag.StringVar(&somevar, "somevar", "data", "a string var to var!")

flag.Parse()

fmt.Println("Name:", *nameFlag)
fmt.Println("Age:", *ageFlag)
fmt.Println("Is alive?", *aliveFlag)
fmt.Println("Somevar", somevar)
fmt.Println("Next args: ", flag.Args())
}

0 comments on commit 02f7194

Please sign in to comment.