diff --git a/channels/go.mod b/channels/go.mod new file mode 100644 index 0000000..f64cff1 --- /dev/null +++ b/channels/go.mod @@ -0,0 +1,3 @@ +module channels + +go 1.17 diff --git a/channels/main.go b/channels/main.go new file mode 100644 index 0000000..a243ddb --- /dev/null +++ b/channels/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + "net/http" + "time" +) + +func main() { + urls := []string{ + "https://google.com", + "https://facebook.com", + "https://golang.org", + "https://johannes-rothe.de", + "https://amazon.com", + } + c := make(chan string) + for _, url := range urls { + go checkURL(url, c) + } + for url := range c { // wait for the channel to return a value, then assign it to l + go func(url string) { + time.Sleep(2 * time.Second) + checkURL(url, c) + }(url) + //go checkURL(url, c) + //fmt.Println(<-c) // value through c is a blocking call + } +} + +func checkURL(url string, c chan string) { + _, err := http.Get(url) + if err != nil { + fmt.Printf("%s might be down!\n", url) + c <- url + return + } + fmt.Printf("%s is up!\n", url) + c <- url +} diff --git a/interfaces_assignment2/go.mod b/interfaces_assignment2/go.mod new file mode 100644 index 0000000..190c1a8 --- /dev/null +++ b/interfaces_assignment2/go.mod @@ -0,0 +1,3 @@ +module interfaces_assignment2 + +go 1.17 diff --git a/interfaces_assignment2/interfaces_assignment2 b/interfaces_assignment2/interfaces_assignment2 new file mode 100755 index 0000000..372e516 Binary files /dev/null and b/interfaces_assignment2/interfaces_assignment2 differ diff --git a/interfaces_assignment2/main.go b/interfaces_assignment2/main.go new file mode 100644 index 0000000..e77cbcd --- /dev/null +++ b/interfaces_assignment2/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "io" + "log" + "os" +) + +func main() { + if len(os.Args) != 2 { + fmt.Printf("No filename given!\n Usage: %s \n", os.Args[0]) + os.Exit(1) + } + filename := os.Args[1] + file, err := os.Open(filename) + if err != nil { + log.Fatal(err) + os.Exit(1) + } + io.Copy(os.Stdout, file) +} diff --git a/interfaces_assignment2/myfile.txt b/interfaces_assignment2/myfile.txt new file mode 100644 index 0000000..980a0d5 --- /dev/null +++ b/interfaces_assignment2/myfile.txt @@ -0,0 +1 @@ +Hello World!