Add remaining
This commit is contained in:
parent
2dbd7ac062
commit
a1558dcce3
7
main.go
7
main.go
@ -50,7 +50,9 @@ import (
|
||||
// "git.sangeeth.dev/gobyexample/httpserver"
|
||||
// "git.sangeeth.dev/gobyexample/httpctx"
|
||||
// "git.sangeeth.dev/gobyexample/exec"
|
||||
"git.sangeeth.dev/gobyexample/signals"
|
||||
// "git.sangeeth.dev/gobyexample/signals"
|
||||
"git.sangeeth.dev/gobyexample/spawn"
|
||||
_ "git.sangeeth.dev/gobyexample/testbench"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -103,5 +105,6 @@ func main() {
|
||||
// httpserver.HttpServer()
|
||||
// httpctx.HttpCtx()
|
||||
// exec.Exec()
|
||||
signals.Signals()
|
||||
// signals.Signals()
|
||||
spawn.Spawn()
|
||||
}
|
||||
|
@ -0,0 +1,55 @@
|
||||
package spawn
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func Spawn() {
|
||||
// Cmd object represents the external process
|
||||
dateCmd := exec.Command("date")
|
||||
|
||||
// Runs and wait for it to finish, output collected
|
||||
dateOut, err := dateCmd.Output()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("> date")
|
||||
fmt.Println(string(dateOut))
|
||||
|
||||
_, err = exec.Command("date", "-x").Output()
|
||||
if err != nil {
|
||||
switch e := err.(type) {
|
||||
case *exec.Error:
|
||||
fmt.Println("failed executing:", err)
|
||||
case *exec.ExitError:
|
||||
fmt.Println("command exit code:", e.ExitCode())
|
||||
default:
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
grepCmd := exec.Command("grep", "hello")
|
||||
|
||||
grepIn, _ := grepCmd.StdinPipe()
|
||||
grepOut, _ := grepCmd.StdoutPipe()
|
||||
grepCmd.Start()
|
||||
grepIn.Write([]byte("hello world\ngoodbye grep\nI am Jin, hello"))
|
||||
grepIn.Close()
|
||||
grepBytes, _ := io.ReadAll(grepOut)
|
||||
grepCmd.Wait()
|
||||
|
||||
fmt.Println("> grep hello")
|
||||
fmt.Println(string(grepBytes))
|
||||
|
||||
lsCmd := exec.Command("bash", "-c", "ls -a -l -h")
|
||||
lsOut, err := lsCmd.Output()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("> ls -a -l -h")
|
||||
fmt.Println(string(lsOut))
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package testbench
|
||||
|
||||
func IntMin(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
|
||||
return b
|
||||
}
|
46
testbench/testbench_test.go
Normal file
46
testbench/testbench_test.go
Normal file
@ -0,0 +1,46 @@
|
||||
package testbench
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIntMinBasic(t *testing.T) {
|
||||
ans := IntMin(2, -2)
|
||||
|
||||
if ans != -2 {
|
||||
t.Errorf("IntMain(2, -2) = %d, want -2", ans)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntMinTableDrive(t *testing.T) {
|
||||
var tests = []struct {
|
||||
a, b int
|
||||
want int
|
||||
}{
|
||||
{0, 1, 0},
|
||||
{1, 0, 0},
|
||||
{2, -2, -2},
|
||||
{0, -1, -1},
|
||||
{-1, 0, -1},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
testName := fmt.Sprintf("%d,%d", tt.a, tt.b)
|
||||
t.Run(testName, func(t *testing.T) {
|
||||
ans := IntMin(tt.a, tt.b)
|
||||
if ans != tt.want {
|
||||
t.Errorf("got %d, want %d", ans, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkIntMin(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
IntMin(1, 2)
|
||||
}
|
||||
}
|
||||
|
||||
// go test -v # to run all tests in current project in verbose mode
|
||||
// go test -bench # to run all benchmarks in current project
|
Loading…
Reference in New Issue
Block a user