Video Coming Soon...
44: curl or wget
This exercise is pending. Quick notes about this exercise:
- My implemention is at https://lcthw.dev/go/go-coreutils
- Not sure which one to go with so try both.
- https://www.gnu.org/software/wget/manual/html_node/index.html
- https://curl.se/
- net -- https://pkg.go.dev/net@go1.25.3
The Code
View Source file go-coreutils/curl/main.go Only
package main
import (
"fmt"
"net/http"
"flag"
"log"
"os"
"io"
"path/filepath"
)
func main() {
var save bool
var headers bool
flag.BoolVar(&save, "O", false, "output file to local disk")
flag.BoolVar(&headers, "I", false, "inspect headers")
flag.Parse()
if flag.NArg() == 0 { log.Fatal("USAGE: curl [-O] [-I] URL") }
target_url := flag.Args()[0]
resp, err := http.Get(target_url)
if err != nil { log.Fatal(err) }
if headers {
for key, values := range resp.Header {
for _, val := range values {
fmt.Printf("%s: %s\n", key, val)
}
}
} else if save {
output := filepath.Base(resp.Request.URL.Path)
out, err := os.Create(output)
if err != nil { log.Fatal(err) }
io.Copy(out, resp.Body)
} else {
io.Copy(os.Stdout, resp.Body)
}
}
Register for Learn Go the Hard Way
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.