I have a PHP based web application. On login page it do ajax call to Go server (located on client machine) to get MAC address. Below is go server code:
package main
import (
"net"
"net/http"
"encoding/json"
)
//define struct for mac address json
type MacAddress struct {
Id string
}
/**
* Get device mac address
*/
func GetMacAddress(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
mac := &MacAddress{Id: ""}
ifas, err := net.Interfaces()
if err != nil {
json.NewEncoder(w).Encode(mac)
return
}
for _, ifa := range ifas {
a := ifa.HardwareAddr.String()
if a != "" {
mac := &MacAddress{Id: a}
json.NewEncoder(w).Encode(mac)
break
}
}
return
}
/**
* Main function
*/
func main() {
http.HandleFunc("/", GetMacAddress)
if err := http.ListenAndServe(":8000", nil); err != nil {
panic(err)
}
}
Result : {Id: "8c:16:45:5h:1e:0e"}
Here i have 2 questions.
-
sometimes i got error “panic: listen tcp :8000: bind: address already in use” and i manually kill the process. So what can be improvement in code to avoid error and close previously running server
-
Can alone go compiled file (created on ubuntu) be run on other systems windows or linux or mac without having go libs and setup
First time on this forum, so if any improvement in way of asking question then please suggest.