Go Simple Server

net/http

Support Http Server

Simple Example

package main

import (
	"fmt"
	"net/http"
)

func request1(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello")
}

func request2(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "World!")
}

func main() {
	server := http.Server{
		Addr: "127.0.0.1:8080",
	}
	http.HandleFunc("/hello", request1)
	http.HandleFunc("/world", request2)
	server.ListenAndServe()
}

This example support 2 endpoint /hello, /world

Two entry point returns plain/text

Test

Use “curl” to check response text

curl localhost:8080/hello
curl localhost:8080/world
golang
スポンサーリンク
Professional Programmer2

コメント