Skip to content

API

You can create an HTTP server using the net/http package, allowing you to handle HTTP requests and responses.

Example

run command
go run src/http/basic_server.go
curl localhost:8080
curl localhost:8080/anonymous_func
package main

import (
    "fmt"
    "net/http"
)

func main() {
    handler := func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello World!"))
    }

    http.HandleFunc("/", handler)
    http.HandleFunc("/anonymous_func", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello World from anonymous_func!"))
    })

    fmt.Println("Server is running on http://localhost:8080")
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        fmt.Println("Error:", err)
    }
}
output
Server is running on http://localhost:8080
Hello World!
Hello World from anonymous_func!

Examples

  1. Get Address - Basic API
  2. Following the Standards for API