Go Libraries
Go libraries for implementing ActivityPub.
go-fed/activity
Comprehensive ActivityPub/ActivityStreams implementation.
| Property | Value |
|---|---|
| Repository | github.com/go-fed/activity |
| Documentation | go-fed.org |
| License | BSD-3 |
| Status | Active |
Features
- Complete ActivityStreams vocabulary
- ActivityPub C2S and S2S
- HTTP Signatures
- Extensible type system
Example
package main
import (
"github.com/go-fed/activity/streams"
"github.com/go-fed/activity/streams/vocab"
)
func createNote() vocab.ActivityStreamsNote {
note := streams.NewActivityStreamsNote()
// Set content
contentProp := streams.NewActivityStreamsContentProperty()
contentProp.AppendXMLSchemaString("Hello from Go!")
note.SetActivityStreamsContent(contentProp)
// Set attributedTo
attrProp := streams.NewActivityStreamsAttributedToProperty()
attrProp.AppendIRI(actorIRI)
note.SetActivityStreamsAttributedTo(attrProp)
return note
}
S2S Federation
import "github.com/go-fed/activity/pub"
// Implement required interfaces
type myActor struct{}
func (a *myActor) PostInbox(c context.Context, w http.ResponseWriter, r *http.Request) (bool, error) {
// Handle incoming activities
}
func (a *myActor) GetOutbox(c context.Context, w http.ResponseWriter, r *http.Request) (bool, error) {
// Return outbox collection
}
go-ap
Collection of ActivityPub packages.
| Property | Value |
|---|---|
| Repository | github.com/go-ap |
| License | MIT |
| Status | Active |
Packages
go-ap/activitypub- Core typesgo-ap/client- HTTP clientgo-ap/handlers- HTTP handlersgo-ap/storage- Storage interface
Example
import (
"github.com/go-ap/activitypub"
)
note := activitypub.Note{
Type: activitypub.NoteType,
Content: activitypub.NaturalLanguageValues{{Value: "Hello!"}},
}
// Marshal to JSON
data, _ := json.Marshal(note)
httpsig
HTTP Signature library.
| Property | Value |
|---|---|
| Repository | github.com/go-fed/httpsig |
| License | BSD-3 |
| Status | Active |
Signing
import "github.com/go-fed/httpsig"
signer, _ := httpsig.NewSigner(
[]httpsig.Algorithm{httpsig.RSA_SHA256},
httpsig.DigestSha256,
[]string{httpsig.RequestTarget, "host", "date", "digest"},
httpsig.Signature,
0,
)
err := signer.SignRequest(privateKey, keyId, req, body)
Verification
verifier, _ := httpsig.NewVerifier(req)
pubKeyId := verifier.KeyId()
// Fetch public key using pubKeyId
err := verifier.Verify(publicKey, httpsig.RSA_SHA256)
webfinger
WebFinger client library.
| Property | Value |
|---|---|
| Repository | github.com/writeas/webfinger |
| License | MIT |
| Status | Stable |
Example
import "github.com/writeas/go-webfinger"
client := webfinger.NewClient(nil)
resource, err := client.Lookup("alice@example.com", nil)
if err != nil {
log.Fatal(err)
}
// Get ActivityPub actor URL
for _, link := range resource.Links {
if link.Rel == "self" &&
link.Type == "application/activity+json" {
fmt.Println(link.Href)
}
}
GoToSocial Source
Study the GoToSocial codebase for production patterns.
| Property | Value |
|---|---|
| Repository | github.com/superseriousbusiness/gotosocial |
| Key Paths | internal/ap/, internal/federation/ |
Notable Patterns
- Clean separation of concerns
- Comprehensive test coverage
- Good documentation
Comparison
| Library | Scope | Complexity | Use Case |
|---|---|---|---|
| go-fed/activity | Full | High | Production servers |
| go-ap | Modular | Medium | Custom implementations |
| httpsig | Signing only | Low | Any project |
Quick Start
For full servers: Use go-fed/activity.
For learning: Study GoToSocial source.
For custom needs: Use go-ap packages.