Skip to content
项目

关于

Official Datastar Go SDK.

开发工具

这些数字如何统计

实测星标增长

24小时
0
7天
+2
15天
+2
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

Datastar Go SDK

Go Reference Go Report Card

This package provides a Go SDK for working with Datastar.

License

This package is licensed for free under the MIT License.

Requirements

This package requires Go 1.24 or later.

Installation

go get github.com/starfederation/datastar-go

Usage

import (
    "net/http"
    "github.com/starfederation/datastar-go/datastar"
)

// Read signals from request
type Store struct {
    Message string `json:"message"`
    Count   int    `json:"count"`
}

func handler(w http.ResponseWriter, r *http.Request) {
    // Read signals from the request
    store := &Store{}
    if err := datastar.ReadSignals(r, store); err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }

    // Create a Server-Sent Event writer
    sse := datastar.NewSSE(w, r)

    // Patch elements in the DOM
    sse.PatchElements(`<div id="output">Hello from Datastar!</div>`)

    // Remove elements from the DOM
    sse.RemoveElement("#temporary-element")

    // Patch signals (update client-side state)
    sse.MarshalAndPatchSignals(map[string]any{
        "message": "Updated message",
        "count":   store.Count + 1,
    })

    // Execute JavaScript in the browser
    sse.ExecuteScript(`console.log("Hello from server!")`)

    // Redirect the browser
    sse.Redirect("/new-page")
}

Examples

See the examples directory for complete working examples.

Testing

go test ./...

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

探索相关项目

加载中…