This adds an experimental package gojs which implements the host side of Wasm compiled by GOARCH=wasm GOOS=js go build -o X.wasm X.go This includes heavy disclaimers, in part inherited by Go's comments https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L10-L11 Due to this many will still use TinyGo instead. That said, this is frequently asked for and has interesting features including reflection and HTTP client support. Signed-off-by: Adrian Cole <adrian@tetrate.io>
33 lines
502 B
Go
33 lines
502 B
Go
package http
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func Main() {
|
|
url := os.Getenv("BASE_URL")
|
|
res, err := http.Get(url + "/error")
|
|
if err == nil {
|
|
log.Panicln(err)
|
|
}
|
|
fmt.Println(err)
|
|
|
|
res, err = http.Post(url, "text/plain", io.NopCloser(strings.NewReader("ice cream")))
|
|
if err != nil {
|
|
log.Panicln(err)
|
|
}
|
|
body, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
log.Panicln(err)
|
|
}
|
|
res.Body.Close()
|
|
|
|
fmt.Println(res.Header.Get("Custom"))
|
|
fmt.Println(string(body))
|
|
}
|