Go - GET HTTP Request Example

In this source code example, we will show how to make a simple GET HTTP request call in Golang.

All Golang source code examples at https://www.sourcecodeexamples.net/p/golang-source-code-examples.html

Go - GET HTTP Request Example

We create a GET request to the webcode.me webpage.

Let's create a file named "go_example.go" and add the following source code to it.

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

func main() {

    resp, err := http.Get("http://webcode.me")

    if err != nil {
        log.Fatal(err)
    }

    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)

    if err != nil {

        log.Fatal(err)
    }

    fmt.Println(string(body))
}

Run the following command to execute the go_example.go file:

Related Golang Source Code Examples


Comments