-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added golang examples with package, exec, envs, flags
- Loading branch information
Showing
6 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module 06a_exec | ||
|
||
go 1.23.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
|