{"id":22327228,"url":"https://github.com/alessandrobessi/statemanager","last_synced_at":"2025-08-24T14:24:40.053Z","repository":{"id":259200611,"uuid":"876576091","full_name":"alessandrobessi/statemanager","owner":"alessandrobessi","description":"Lightweight, Flexible State Management for Go Applications! 🚀","archived":false,"fork":false,"pushed_at":"2024-10-22T08:45:55.000Z","size":24,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-31T07:45:01.692Z","etag":null,"topics":["hacktoberfest","hacktoberfest2024","sessionmanager","statemanager"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alessandrobessi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-22T07:52:48.000Z","updated_at":"2024-10-22T09:25:23.000Z","dependencies_parsed_at":"2024-10-23T13:04:14.878Z","dependency_job_id":null,"html_url":"https://github.com/alessandrobessi/statemanager","commit_stats":null,"previous_names":["alessandrobessi/statemanager"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alessandrobessi%2Fstatemanager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alessandrobessi%2Fstatemanager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alessandrobessi%2Fstatemanager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alessandrobessi%2Fstatemanager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alessandrobessi","download_url":"https://codeload.github.com/alessandrobessi/statemanager/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245599370,"owners_count":20642088,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","hacktoberfest2024","sessionmanager","statemanager"],"created_at":"2024-12-04T03:08:49.417Z","updated_at":"2025-03-26T06:22:33.125Z","avatar_url":"https://github.com/alessandrobessi.png","language":"Go","readme":"# statemanager\n**Lightweight, Flexible State Management for Go Applications!** 🚀\n\nThis is a Go module that provides easy file-based state management with support for user-defined state structures. It’s designed for applications that need to persist state across multiple invocations by writing the state to a file.\n\nWith this module, users can define their own state struct, and the state manager will handle loading and saving the state to a file in JSON format.\n\n ## Features\n    - Generic state management: Define your own state structure using Go's generics.\n    - File-based persistence: State is saved to and loaded from a file in JSON format.\n    - Lightweight: No external dependencies are required.\n\n## Installation\n\nTo install the module, use the go get command:\n```bash\n\ngo get github.com/alessandrobessi/statemanager\n```\nThen, import it in your Go code:\n\n```go\n\nimport \"github.com/alessandrobessi/statemanager\"\n```\n\n## Quick Start\n1. Define Your State Struct\n\nYou can define your own state structure based on your application’s needs.\n\n```go\n\ntype MyState struct {\n    Count int    `json:\"count\"`\n    Name  string `json:\"name\"`\n}\n```\n2. Create and Use the State Manager\n\nYou can then create an instance of the StateManager, providing the path to the state file and an initial state. The module will load the state from the file or initialize it with the provided state if the file doesn’t exist.\n\n```go\n\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/alessandrobessi/statemanager\"\n)\n\ntype MyState struct {\n    Count int    `json:\"count\"`\n    Name  string `json:\"name\"`\n}\n\nfunc main() {\n    initialState := MyState{Count: 0, Name: \"My App\"}\n    sm := statemanager.NewStateManager(\"state.json\", initialState)\n\n    // Access and update the state\n    myState := sm.State\n    myState.Count++\n    fmt.Printf(\"Name: %s, Invocation Count: %d\\n\", myState.Name, myState.Count)\n\n    // Save the updated state\n    if err := sm.SaveState(); err != nil {\n        fmt.Println(\"Error saving state:\", err)\n    }\n}\n```\n\n3. Running the Program\n\nEach time the program is invoked, it will:\n\n    - Load the state from state.json.\n    - Increment the Count field.\n    - Save the updated state back to state.json.\n\n4. Customizing the State Structure\n\nThe StateManager uses Go generics, so you can define any struct to hold your state and the module will work seamlessly with it.\n\n```go\n\ntype AnotherState struct {\n    LastRun string `json:\"last_run\"`\n    Count   int    `json:\"count\"`\n}\n```\nYou can create a new StateManager with this state structure just as easily:\n\n```go\n\nsm := statemanager.NewStateManager(\"another_state.json\", AnotherState{Count: 0, LastRun: \"never\"})\n```\n\n## API Reference\n- `NewStateManager(filePath string, initialState T)`\n\nCreates a new StateManager instance.\n\n    - filePath (string): The path to the file where the state is stored.\n    - initialState (T): The initial state of your application. This can be any struct.\n\n- `LoadState() error`\n\nLoads the state from the file. If the file doesn’t exist, it will continue using the provided initial state.\n\n- `SaveState() error`\n\nSaves the current state to the file. The state is serialized into JSON format.\n\n## Testing\n\nTo run the tests for the module, use the following command:\n\n```bash\n\ngo test ./...\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falessandrobessi%2Fstatemanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falessandrobessi%2Fstatemanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falessandrobessi%2Fstatemanager/lists"}