a buttload of new experiments

This commit is contained in:
Greg Gauthier 2021-03-08 17:50:01 +00:00
parent c11748bf06
commit 3859176a52
11 changed files with 375 additions and 0 deletions

30
channel-close.go Normal file
View File

@ -0,0 +1,30 @@
package main
import "fmt"
func main() {
jobs := make(chan int, 5)
done := make(chan bool)
go func() {
for {
j, more := <-jobs
if more {
fmt.Println("received job", j)
} else {
fmt.Println("received all jobs")
done <- true
return
}
}
}()
for j := 1; j <= 3; j++ {
jobs <- j
fmt.Println("sent job", j)
}
close(jobs)
fmt.Println("sent all jobs")
<-done
}

20
channel-directions.go Normal file
View File

@ -0,0 +1,20 @@
package main
import "fmt"
func ping(pings chan<- string, msg string) {
pings <- msg
}
func pong(pings <-chan string, pongs chan<- string) {
msg := <-pings
pongs <- msg
}
func main() {
pings := make(chan string, 1)
pongs := make(chan string, 1)
ping(pings, "passed message")
pong(pings, pongs)
fmt.Println(<-pongs)
}

34
channel-sync.go Normal file
View File

@ -0,0 +1,34 @@
package main
import (
"fmt"
"time"
)
func signal() chan bool {
return make(chan bool, 1)
}
func worker(done chan bool, wkrnum int) {
fmt.Println("BEGIN: ", wkrnum)
fmt.Println("Wait 5 seconds...", wkrnum)
time.Sleep(time.Duration(5 * time.Second))
fmt.Printf("%d plus %d equals %d\n", wkrnum, 5, 5+wkrnum)
time.Sleep(time.Duration(5000))
fmt.Println("END: ", wkrnum)
done <- true
}
func main() {
done1 := signal()
go worker(done1, 1)
<-done1
done2 := signal()
go worker(done2, 2)
<-done2
done3 := signal()
go worker(done3, 3)
<-done3
}

49
channels.go Normal file
View File

@ -0,0 +1,49 @@
package main
import (
"fmt"
"strings"
)
func simpleping() {
messages := make(chan string)
go func() {
messages <- "ping"
}()
msg := <-messages
fmt.Println(msg)
if msg == "ping" {
fmt.Println("pong")
}
}
func snd(msgchan chan string, msgs []string) {
for i := 0; i < len(msgs); i++ {
msgchan <- msgs[i]
}
}
func rcv(msgchan chan string) string {
var sentence []string
var msglen int = len(msgchan)
for v := range msgchan {
fmt.Println(len(sentence), sentence)
if len(sentence) == msglen-1 { // when we reach next to last in the array, do the last.
sentence = append(sentence, v)
break
} else {
sentence = append(sentence, v)
}
}
return strings.Join(sentence, " ")
}
func main() {
simpleping()
var words = []string{"buffered", "channel", "one", "two", "three", "four", "five", "six"}
msgchan := make(chan string, len(words))
snd(msgchan, words)
fmt.Println(rcv(msgchan))
}

BIN
check-versions Executable file

Binary file not shown.

40
check-versions.go Normal file
View File

@ -0,0 +1,40 @@
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
)
func validateos() {
if runtime.GOOS == "windows" {
fmt.Println("Can't Execute this on a windows machine")
os.Exit(1)
}
}
func execute(cmdstr string) {
validateos()
var cmdargs = strings.Split(cmdstr, " ") // string arrayified
var cmd = cmdargs[0] // command
cmdargs = append(cmdargs[:0], cmdargs[1:]...) // argument array sans cmd
out, err := exec.Command(cmd, cmdargs...).CombinedOutput()
if err != nil {
fmt.Println(err)
}
output := string(out[:])
fmt.Println(output)
}
func main() {
execute("/usr/sbin/lsof -iTCP -sTCP:LISTEN")
execute("gcc --version")
execute("java -version")
execute("python3 --version")
execute("php --version")
}

57
handlers.go Normal file
View File

@ -0,0 +1,57 @@
package main
import (
"errors"
"fmt"
)
func f1(arg int) (int, error) {
if arg == 42 {
return -1, errors.New("can't work with 42")
}
return arg + 3, nil
}
type argError struct {
arg int
prob string
}
func (e *argError) Error() string {
return fmt.Sprintf("%d - %s", e.arg, e.prob)
}
func f2(arg int) (int, error) {
if arg == 42 {
return -1, &argError{arg, "can't work with it"}
}
return arg + 3, nil
}
func main() {
for _, i := range []int{7, 42} {
if r, e := f1(i); e != nil {
fmt.Println("f1 failed:", e)
} else {
fmt.Println("f1 worked:", r)
}
}
for _, i := range []int{7, 42} {
if r, e := f2(i); e != nil {
fmt.Println("f2 failed:", e)
} else {
fmt.Println("f2 worked:", r)
}
}
_, e := f2(42)
if ae, ok := e.(*argError); ok {
fmt.Println(ae.arg)
fmt.Println(ae.prob)
}
}

49
interfaces.go Normal file
View File

@ -0,0 +1,49 @@
package main
import (
"fmt"
"math"
)
// I have changed the names of the types and interface methods because they conflict with
// the go native library
type geometry interface {
areaa() float64
perimm() float64
}
type recta struct {
width, height float64
}
type circle struct {
radius float64
}
func (r recta) areaa() float64 {
return r.width * r.height
}
func (r recta) perimm() float64 {
return 2*r.width + 2*r.height
}
func (c circle) areaa() float64 {
return math.Pi * c.radius * c.radius
}
func (c circle) perimm() float64 {
return 2 * math.Pi * c.radius
}
func measure(g geometry) {
fmt.Println(g)
fmt.Println(g.areaa())
fmt.Println(g.perimm())
}
func main() {
r := recta{width: 3, height: 4}
c := circle{radius: 5}
measure(r)
measure(c)
}

32
non-blocks.go Normal file
View File

@ -0,0 +1,32 @@
package main
import "fmt"
func main() {
messages := make(chan string)
signals := make(chan bool)
select {
case msg := <-messages:
fmt.Println("received message", msg)
default:
fmt.Println("no message received")
}
msg := "hi"
select {
case messages <- msg:
fmt.Println("sent message", msg)
default:
fmt.Println("no message sent")
}
select {
case msg := <-messages:
fmt.Println("received message", msg)
case sig := <-signals:
fmt.Println("received signal", sig)
default:
fmt.Println("no activity")
}
}

30
select-switch.go Normal file
View File

@ -0,0 +1,30 @@
package main
import (
"fmt"
"time"
)
func main() {
c1 := make(chan string)
c2 := make(chan string)
go func() {
time.Sleep(1 * time.Second)
c1 <- "one"
}()
go func() {
time.Sleep(2 * time.Second)
c2 <- "two"
}()
for i := 0; i < 2; i++ {
select {
case msg1 := <-c1:
fmt.Println("received", msg1)
case msg2 := <-c2:
fmt.Println("received", msg2)
}
}
}

34
timeouts.go Normal file
View File

@ -0,0 +1,34 @@
package main
import (
"fmt"
"time"
)
func main() {
c1 := make(chan string, 1)
go func() {
time.Sleep(5 * time.Second)
c1 <- "channel 1 response!"
}()
select {
case res := <-c1:
fmt.Println(res)
case <-time.After(3 * time.Second):
fmt.Println("channel 1 timeout!")
}
c2 := make(chan string, 1)
go func() {
time.Sleep(2 * time.Second)
c2 <- "channel 2 response!"
}()
select {
case res := <-c2:
fmt.Println(res)
case <-time.After(3 * time.Second):
fmt.Println("timeout 2")
}
}