repositorypackage
0.0.0-20241228072030-0c81712fd9cb
Repository: https://github.com/jcbhmr/go-fetch.git
Documentation: pkg.go.dev
# README
Fetch API for Go
š„ The WHATWG Fetch API for Go
|
Installation
You can install this package using go get
right from your command line:
go get github.com/jcbhmr/go-fetch
Or if you prefer you can import it in your Go code and use go mod tidy
to automagically ⨠add it to your go.mod
.
import "github.com/jcbhmr/go-fetch"
Usage
package main
import (
"log"
"github.com/jcbhmr/go-fetch"
)
func main() {
response, err := fetch.Fetch("https://example.com/")
if err != nil {
log.Fatal(err)
}
text, err := response.Text()
if err != nil {
log.Fatal(err)
}
log.Println("%s returned this %s:", response.URL(), response.Headers().Get("Content-Type"))
log.Println(text)
}
Development
WebIDL's primary language integration is between C++ and JavaScript but a lot of the constructs remain relatively portable. In this case, though, we are implementing & consuming the Fetch API using Go! That means there's some things to be aware of about how the WebIDL-defined API is translated into somewhat idiomatic Go code.
- Names are transformed into Go-conformant PascalCase names to be properly exported.
optional
parameters becomenil
-able*T
types if the original type is un-nil
-able (likestring
,int
, etc.). If the original type isnil
-able then it stays as-is (like a*MyStruct
parameter).- Sum types that can't be shoehorned in using sealed
interface
hacks (likeMyStruct|string
since you can't do interfaces on primitives) are defined asany
and annotated with a developer note. DOMString
andUSVString
are both represented using the Gostring
primitive. They are mostly considered interchangable. This might be changed later. š¤·āāļøByteString
is also just a Gostring
.- JavaScript-native
Promise<T>
has been replaced with a<-chan Result[T]
BufferSource
where possible is a[]byte|[]uint8|[]int8|[]uint16|...
unionArrayBuffer
is considered to be equivalent to[]byte
- Go versions of WebIDL enums rely on the user to know the possible enum values. So
type RequestCredentials = string
. - All WebIDL-defined properties are exposed through getter and setter methods. There is no prefix for getters and a
Set
prefix for setters. For instancemyStruct.Value()
andmyStruct.SetValue()
would be the getter/setter forMyStruct#value
. - This package does expose non-
[Exposed=...]
WebIDL types that are not available to JavaScript. - WebIDL
dictionary
structs always have all their fields madenil
-able similar tooptional
parameters unless they are markedrequired
. iterable<K,V>
means that it has an.Iter()
method that returns aSeq2[K,V]
function which can be used with https://go.dev/wiki/RangefuncExperimentiterable<V>
means that it has an.Iter()
method that returns aSeq[V]
function which can be used with https://go.dev/wiki/RangefuncExperiment- In places where a
TypeError
would be thrown it willpanic()
- In places where a
DOMException
would be thrown it will return anerror
sequence<T>
is taken to be equivalent to[]T