프로젝트
GitHub에서 보기
starfederation/datastar-go
소개
Official Datastar Go SDK.
개발 도구측정된 스타 증가
- in 24h
- 0
- in 7 days
- +2
- in 15 days
- +2
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
Original
Datastar Go SDK
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.
관련 프로젝트 살펴보기
불러오는 중…