Git Product home page Git Product logo

readonly's Introduction

Go Package readonly

Overview

The readonly package provides a simple and efficient way to handle immutable and final values in Go. It offers two main types: ReadOnly and Final, each designed to manage values that are either immutable or assigned once.

Installation

To use the readonly package in your Go project, simply import it as follows:

import "github.com/STRockefeller/readonly"

Usage

The ReadOnly Struct

The ReadOnly struct is used to create immutable values. Once a value is set, it cannot be changed.

Creating a ReadOnly Instance

value := readonly.NewReadOnly(42)

Accessing the Value

v := value.Get()

The Final Struct

The Final struct allows for creating a value that can be set only once. It's an alternative to ReadOnly for scenarios where the value might not be known at the time of instantiation.

Creating a Final Instance

var finalValue readonly.Final[int]

Setting the Value

To set the value for the first time:

finalValue.Set(42)

Accessing the Value

To safely access the value:

v := finalValue.Get()

Features

  • Immutable value handling with ReadOnly.
  • Single-assignment value handling with Final.
  • Type-safe implementation using Go generics.

License

MIT License

readonly's People

Contributors

strockefeller avatar

Stargazers

FAN VINGA avatar zllangct avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.