Golang: Quick Start - cross compile & local server
install (on mac)
% brew install go
% go version
go version go1.16.3 darwin/amd64
run
% cat hello.go
package main
import "fmt"
func main() {
fmt.Printf("Hello, World!\n")
}
% go run hello.go
Hello, World!
% cat hello.go
package main
import "fmt"
func main() {
fmt.Printf("Hello, World!\n")
}
% go run hello.go
Hello, World!
compile
% go build hello.go
> hello is created
% ./hello
Hello World
cross compile
# get the list of available values
% go tool dist listaix/ppc64
android/386
android/amd64
...
# compile for windows/amd64% GOOS=windows GOARCH=amd64 go build hello.go
> hello.exe is created
server
% cat server.go
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
% go run server.go
% curl http://localhost:8080/
Hello, World
Comments
Post a Comment