Home / File/ state.go — fiber Source File

state.go — fiber Source File

Architecture documentation for state.go, a go file in the fiber codebase. 1 imports, 0 dependents.

File go FiberCore Context 1 imports 7 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  5bc5a845_0556_f27f_dfa4_49534ad22ad5["state.go"]
  fa066dfe_6315_0cba_c8fe_7f7cddd98675["hex"]
  5bc5a845_0556_f27f_dfa4_49534ad22ad5 --> fa066dfe_6315_0cba_c8fe_7f7cddd98675
  style 5bc5a845_0556_f27f_dfa4_49534ad22ad5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

package fiber

import (
	"encoding/hex"
	"strings"
	"sync"

	"github.com/google/uuid"
)

const servicesStatePrefix = "gofiber-services-"

var servicesStatePrefixHash string

func init() {
	servicesStatePrefixHash = hex.EncodeToString([]byte(servicesStatePrefix + uuid.New().String()))
}

// State is a key-value store for Fiber's app in order to be used as a global storage for the app's dependencies.
// It's a thread-safe implementation of a map[string]any, using sync.Map.
type State struct {
	dependencies  sync.Map
	servicePrefix string
}

// NewState creates a new instance of State.
func newState() *State {
	// Initialize the services state prefix using a hashed random string
	return &State{
		dependencies:  sync.Map{},
		servicePrefix: servicesStatePrefixHash,
	}
}

// Set sets a key-value pair in the State.
func (s *State) Set(key string, value any) {
	s.dependencies.Store(key, value)
}

// Get retrieves a value from the State.
func (s *State) Get(key string) (any, bool) {
	return s.dependencies.Load(key)
}

// MustGet retrieves a value from the State and panics if the key is not found.
func (s *State) MustGet(key string) any {
	if dep, ok := s.Get(key); ok {
		return dep
	}

	panic("state: dependency not found!")
}

// Has checks if a key is present in the State.
// It returns a boolean indicating if the key is present.
func (s *State) Has(key string) bool {
	_, ok := s.Get(key)
	return ok
}

// ... (260 more lines)

Domain

Subdomains

Types

Dependencies

  • hex

Frequently Asked Questions

What does state.go do?
state.go is a source file in the fiber codebase, written in go. It belongs to the FiberCore domain, Context subdomain.
What functions are defined in state.go?
state.go defines 7 function(s): GetService, GetState, GetStateWithDefault, MustGetService, MustGetState, init, newState.
What does state.go depend on?
state.go imports 1 module(s): hex.
Where is state.go in the architecture?
state.go is located at state.go (domain: FiberCore, subdomain: Context).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free