Welcome to the new Golem Cloud Docs! 👋
Documentation
Go Language Guide
HTTP client

HTTP requests in Go

Golem implements the WASI HTTP (opens in a new tab) interfaces so any library built on this specification can be used from Golem components to communicate with external services.

By using the Golem Go SDK, Go's built-in net/http package can be configured to use the WASI HTTP interfaces.

To do so, Golem's custom Roundtrip implementation hast to be used. This can be done directly before making http requests:

import (
	"net/http"
 
	"github.com/golemcloud/golem-go/roundtrip"
)
 
http.DefaultClient.Transport = roundtrip.WasiHttpTransport{};

Or the convenience std package can be used at the start of component methods (which can also initialize commandline arguments and environment variable):

package main
 
import (
	"fmt"
	"net/http"
	"os"
 
	// import the std helper
	"github.com/golemcloud/golem-go/std"
 
	"golem/demo/go_example"
)
 
type GoExampleImpl struct {
}
 
func (e *GoExampleImpl) HelloWorld() string {
    // initialize the standard library's os and http package from the golem environment
	// this initialization has to be called at the start of the implementation methods
	std.Init(std.Packages{
		Os: true,
		NetHttp: true,
	})
 
	// The standard http package can be used to make http requests
	_, _ = http.Get("https://host/endpoint")
 
	return "Hello, World!"
}
 
func init() {
	go_example.SetGoExample(&GoExampleImpl{})
}