View on GitHub

Go Lang Notes

Compilations of Go Lang sample code along with AWS examples. Topped with notes on related topics.

Simple Go Lang Server

Visitors

Simple Web Server in Go

You can create a simple web server in go using "net/http" package. In which you can use http.HandleFunc() to handle your request.

package main

import(
    "fmt"
    "net/http"
)

func hello(w http.ResponseWriter, req *http.Request) {
    name := req.URL.Query().Get("name")
    message := fmt.Sprintf("Hello %s", name)
    byteResponse := []byte(message)
    w.Write(byteResponse)
}

func main() {
    pattern := "/hello"
    http.HandleFunc(pattern, hello)
    http.ListenAndServe(":8080", nil)
}

Command to Test

# Powershell Command
$response = Invoke-RestMethod 'http://localhost:8080/hello?name=Akash' -Method 'GET' -Headers $headers
$response | ConvertTo-Json

# Command Prompt Command
curl http://localhost:8080/hello?name=Akash

Powershell Command to Test

Below powershell command will help you test the code written in main.go

$response = Invoke-RestMethod 'http://localhost:8090/get-sample' -Method 'GET' -Headers $headers
$response | ConvertTo-Json

Visitors