Skip to main content

get client ip

· One min read
package main

import (
"io"
"log"
"net/http"

"github.com/thinkeridea/go-extend/exnet"
)

func main() {

http.HandleFunc("/", ipHandler)
log.Println("server is running at: http://127.0.0.1:10086")
//block
http.ListenAndServe(":10086", nil)
}

func ipHandler(w http.ResponseWriter, r *http.Request) {
ip := exnet.ClientPublicIP(r)
if ip == "" {
ip = exnet.ClientIP(r)
}
// log.Println(getMyIPV6())
io.WriteString(w, ip)
}