Success or failure?

Report a typo

Edwin wants to create a program that interacts with a web server to test his knowledge of making HTTP requests using Go.

His program must take as input a certain url of a web server, and then use http.Get() to send a GET request to the URL. It should return "Success" and the StatusCode if the request succeeds, and "Fail" and the StatusCode otherwise.

Remember that the status codes between 200 and 299 indicate that the request has succeeded, while all the codes starting with a 4 signalize an error.

Sample Input 1:

http://127.0.0.1:8080

Sample Output 1:

Success 200
Write a program in Go
package main

import (
"fmt"
"net/http"
"time"
)

func main() {
var url string
fmt.Scanln(&url)

// Make a GET request to the 'url' variable below:
response, err := ?
if err != nil {
panic(err)
}
defer ? // What should we always 'Close'?

// Write an if statement to check for response codes between 200 and 299:
if ?.StatusCode >= ? && ? {
if err != nil {
panic(err)
}
fmt.Println("?", ?) // If we have a 2XX response what should we print?
} else {
fmt.Println("?", ?) // If we have a 4XX response what should we print?
}
}

// DO NOT MODIFY the contents of the init() or must() functions!
func init() {
const timeout = 5 * time.Second

go func() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
___

Create a free account to access the full topic