Skip to content

Instantly share code, notes, and snippets.

@boseabhishek
Last active May 23, 2024 12:59
Show Gist options
  • Save boseabhishek/a57a2680bb93cc653acd2795d166bb45 to your computer and use it in GitHub Desktop.
Save boseabhishek/a57a2680bb93cc653acd2795d166bb45 to your computer and use it in GitHub Desktop.
Learning Go!
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
if len(os.Args) == 1 {
fmt.Println("oops! try again")
os.Exit(1)
}
url := os.Args[1]
cmd := exec.Command("curl", "-s", url)
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("Error:", err)
fmt.Println("Output:", string(output))
return
}
fmt.Println(string(output))
// iteration: 2
// NOT using exec.Command
// Still, same output
// {
// "userId": 1,
// "id": 1,
// "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
// "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
// }
}
// how to execute:
// create a go.mod
// $ go mod init curly
// for every change:
// $ go build ./...
// or
// $ go install ./...
// run:
// $ ./curly <url>
// or
// $ curly <url>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment