done

package module
v1.0.28 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2025 License: MIT Imports: 4 Imported by: 28

Image README

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

Done - Simple Error Handling in Go

Done lets you focus on the business logic without repetitive if err != nil patterns.

When you write logic:

if err := run(); err != nil {
    panic(err)
}

Then you can use done:

done.Done(run())

CHINESE README

中文说明


Features

  • Eliminates boilerplate: Replace verbose if err != nil blocks with concise function calls
  • Type-safe validation: Built with Go generics to provide compile-time type checking and IDE support
  • Chainable operations: Link validation calls into clean, readable code flow
  • Multiple return types: Handle functions returning values, pointers, slices, maps, and more
  • Rich assertions: Specialized validators covering numbers, strings, booleans, and collections
  • Focus on logic: Keep error handling lightweight so business logic stands out

Installation

go get github.com/yyle88/done

Usage

Service Chain Validation

This example demonstrates three approaches: classic step-based validation, chained validation, and compact chained validation using done.

package main

import (
	"fmt"

	"github.com/yyle88/done"
)

func main() {
	// Classic approach with explicit checks
	fmt.Println(WithClassicErrorHandling())

	// Chained approach with done.VCE
	fmt.Println(WithChainedErrorHandling())

	// Compact chained approach
	fmt.Println(WithCompactChainedHandling())
}

// WithClassicErrorHandling demonstrates classic validation with explicit checks.
// Pros: Distinct and simple to debug; Cons: Verbose code.
func WithClassicErrorHandling() string {
	service, err := NewService()
	if err != nil {
		panic(err) // Handle errors at each step
	}
	client, err := service.GetClient()
	if err != nil {
		panic(err)
	}
	response, err := client.GetResponse()
	if err != nil {
		panic(err)
	}
	return response.Message
}

// WithChainedErrorHandling uses done.VCE to streamline validation in a chained fashion.
// Pros: Compact code; Cons: Debugging stacks can be challenging.
func WithChainedErrorHandling() string {
	service := done.VCE(NewService()).Nice()
	client := done.VCE(service.GetClient()).Nice()
	response := done.VCE(client.GetResponse()).Nice()
	return response.Message
}

// WithCompactChainedHandling shows the most compact form of chained validation.
// Pros: Concise code; Cons: Debugging is challenging.
func WithCompactChainedHandling() string {
	return done.VCE(done.VCE(done.VCE(
		NewService(),
	).Nice().GetClient(),
	).Nice().GetResponse(),
	).Nice().Message
}

// Service represents the main service in the chain.
type Service struct{}

// NewService creates a new Service instance.
func NewService() (*Service, error) {
	return &Service{}, nil
}

// GetClient returns a Client instance from this service.
func (s *Service) GetClient() (*Client, error) {
	return &Client{}, nil
}

// Client represents the intermediate client in the chain.
type Client struct{}

// GetResponse returns the Response containing the result message.
func (c *Client) GetResponse() (*Response, error) {
	return &Response{
		Message: "success", // Simulated success message
	}, nil
}

// Response represents the response containing the result.
type Response struct {
	Message string // Result message
}

⬆️ Source: Source

Chaining Operations

This example demonstrates two approaches to error handling: classic step-based validation and compact chained validation using done.

package main

import (
	"fmt"
	"strconv"

	"github.com/pkg/errors"
	"github.com/yyle88/done"
)

func main() {
	// Classic approach with explicit checks
	fmt.Println(WithClassicErrorHandling())

	// Compact chained approach
	fmt.Println(WithCompactChainedHandling())
}

// WithClassicErrorHandling demonstrates classic validation with explicit checks.
// Pros: Distinct and explicit; Cons: Verbose code.
func WithClassicErrorHandling() int64 {
	text, err := webFetch()
	if err != nil {
		panic(err) // Handle error at each step
	}
	num, err := parseNum(text)
	if err != nil {
		panic(err)
	}
	if num <= 0 {
		panic(errors.New("num must be positive"))
	}
	return num
}

// WithCompactChainedHandling uses done.VCE and done.VNE to provide concise validation.
// Pros: Compact code; Cons: Debugging can be challenging.
func WithCompactChainedHandling() int64 {
	// Chain methods to fetch, parse, and validate the value
	return done.VNE(
		parseNum(
			done.VCE(webFetch()).Nice(),
		),
	).Gt(0)
}

// webFetch simulates fetching a string value from a remote source.
func webFetch() (string, error) {
	return "100", nil // Simulated data fetch
}

// parseNum converts a string to an int64 value.
func parseNum(text string) (int64, error) {
	return strconv.ParseInt(text, 10, 64)
}

⬆️ Source: Source

Pointer Validation

This example demonstrates using done.P2 to validate and extract multiple pointer results.

package main

import (
	"fmt"

	"github.com/yyle88/done"
)

func main() {
	// Classic approach: check results with explicit validation
	WithClassicErrorHandling()

	// Compact approach: use done.P2 to reduce boilerplate
	WithCompactErrorHandling()
}

// WithClassicErrorHandling demonstrates classic validation with explicit checks.
// Pros: Explicit and distinct; Cons: Verbose code.
func WithClassicErrorHandling() {
	account, config, err := fetchAccountAndConfig()
	if err != nil {
		panic(err) // Check each step
	}
	if account == nil {
		panic("account is nil") // Validate account exists
	}
	if config == nil {
		panic("config is nil") // Validate config exists
	}
	fmt.Println(account, config) // Print both account and config
}

// WithCompactErrorHandling uses done.P2 to streamline validation.
// Pros: Concise code; Cons: Validation is implicit.
func WithCompactErrorHandling() {
	account, config := done.P2(fetchAccountAndConfig()) // done.P2 handles checks
	fmt.Println(account, config)                        // Print both account and config
}

// Account represents an account in the system.
type Account struct {
	ID   int    // Account ID
	Name string // Account name
}

// Config represents configuration settings.
type Config struct {
	Timeout int    // Timeout in seconds
	Region  string // Service region
}

// fetchAccountAndConfig simulates fetching account and config data.
func fetchAccountAndConfig() (*Account, *Config, error) {
	account := &Account{ID: 1, Name: "Alice"}
	config := &Config{Timeout: 30, Region: "us-west"}
	return account, config, nil
}

⬆️ Source: Source


Core Types

Type Description
Ve[V any] Wraps any value with error. Provides Done, Must, Soft to handle errors
Vpe[V any] Wraps pointer with error. Validates non-nil with Sure, Nice, Full
Vce[V comparable] Wraps comparable value with error. Compares values using Same, Diff, Equals
Vbe Wraps boolean with error. Asserts true/false with TRUE, FALSE, OK, NO
Vae[V any] Wraps slice with error. Checks emptiness and length with Some, Have, Length
Vme[K, V] Wraps map with error. Validates size and content with Nice, Size, Len

Core Functions

Function Description
Done Panics with logging if error exists
Must Ensures error is nil, returns value on success
Soft Logs warning without panic, continues execution
Fata Logs fatal error and terminates program

Function Groups

Group Functions Description
Errors Done, Must, Soft Handle errors with panic/warning based on stage
Non-Zero Validation Sure, Nice, Some Validate non-zero values and return them
Non-Zero Assertion Good, Fine, Safe Assert non-zero values without returning
Zero Value Checking Zero, None, Void Validate values are zero/absent
Value Comparisons Same, Diff, Is, Equals Compare values checking matches/differences

Type-Specific Validation

Type Purpose Methods
Vce Comparable values Same, Diff, Is, Equals - value comparison
Vse String operations HasPrefix, HasSuffix, Contains - substring checks
Vne Numeric comparisons Gt, Lt, Gte, Lte - range validation
Vbe Boolean assertions TRUE, FALSE, YES, NO, OK - true/false checks
Vae Slice validation Sure, Some, Have, Length - emptiness and size checks
Vme Map validation Nice, Some, Size, Len - map size and content checks

Examples

Basic Error Handling

Simple error check:

done.Done(run())

Return value with error check:

result := done.V1(fetchData())

Multiple return values:

v1, v2 := done.V2(getTwoValues())
Pointer Validation

Single pointer validation:

ptr := done.P1(getPointer())

Multiple pointer validation:

ptr1, ptr2 := done.P2(getTwoPointers())
Comparable Value Operations

Check values are the same:

value := done.VCE(getValue()).Same(expected)

Check values are different:

value := done.VCE(getValue()).Diff(unwanted)
Numeric Comparisons

Validate value exceeds threshold:

num := done.VNE(getNumber()).Gt(0)

Less than validation:

num := done.VNE(getNumber()).Lt(100)
Boolean Validation

Ensure boolean is true:

done.VBE(checkCondition()).TRUE()

Ensure boolean is false:

done.VBE(checkCondition()).FALSE()
Slice Operations

Ensure slice has elements:

items := done.VAE(getSlice()).Some()

Check slice length:

items := done.VAE(getSlice()).Length(3)
Map Operations

Ensure map has content:

data := done.VME(getMap()).Nice()

Check map size:

data := done.VME(getMap()).Size(5)

Conclusion

The Done package brings a robust approach to error handling in Go. Through eliminating repetitive error checks, it lets you write clean, maintainable code. When building prototypes and production systems, Done helps you focus on what matters: the business logic.

Test it out and let us know what you think!


Explore more error handling packages in this ecosystem:

Advanced Packages
  • must - Must-style assertions with rich type support and detailed error context
  • rese - Result extraction with panic, focused on safe value unwrapping
Foundation Packages
  • done - Simple, focused error handling (this project)
  • sure - Generates code that creates custom validation methods

Each package targets different use cases, from quick prototyping to production systems with comprehensive error handling.


📄 License

MIT License. See LICENSE.


🤝 Contributing

Contributions are welcome! Report bugs, suggest features, and contribute code:

  • 🐛 Found a mistake? Open an issue on GitHub with reproduction steps
  • 💡 Have a feature idea? Create an issue to discuss the suggestion
  • 📖 Documentation confusing? Report it so we can improve
  • 🚀 Need new features? Share the use cases to help us understand requirements
  • Performance issue? Help us optimize through reporting slow operations
  • 🔧 Configuration problem? Ask questions about complex setups
  • 📢 Follow project progress? Watch the repo to get new releases and features
  • 🌟 Success stories? Share how this package improved the workflow
  • 💬 Feedback? We welcome suggestions and comments

🔧 Development

New code contributions, follow this process:

  1. Fork: Fork the repo on GitHub (using the webpage UI).
  2. Clone: Clone the forked project (git clone https://github.com/yourname/repo-name.git).
  3. Navigate: Navigate to the cloned project (cd repo-name)
  4. Branch: Create a feature branch (git checkout -b feature/xxx).
  5. Code: Implement the changes with comprehensive tests
  6. Testing: (Golang project) Ensure tests pass (go test ./...) and follow Go code style conventions
  7. Documentation: Update documentation to support client-facing changes and use significant commit messages
  8. Stage: Stage changes (git add .)
  9. Commit: Commit changes (git commit -m "Add feature xxx") ensuring backward compatible code
  10. Push: Push to the branch (git push origin feature/xxx).
  11. PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.

Please ensure tests pass and include relevant documentation updates.


🌟 Support

Welcome to contribute to this project via submitting merge requests and reporting issues.

Project Support:

  • Give GitHub stars if this project helps you
  • 🤝 Share with teammates and (golang) programming friends
  • 📝 Write tech blogs about development tools and workflows - we provide content writing support
  • 🌟 Join the ecosystem - committed to supporting open source and the (golang) development scene

Have Fun Coding with this package! 🎉🎉🎉


GitHub Stars

starring

Image Documentation

Overview

Package done: Simple error handling package with panic-based validation Auto panics on errors to simplify error checking patterns in Go code Supports validation functions that reduce boilerplate error handling Provides type-safe validation methods with inline assertions

done: Go 错误处理简化包,基于 panic 的验证机制 错误时自动 panic,简化 Go 代码中的错误检查模式 支持验证函数,减少样板错误处理代码 提供类型安全的验证方法和内联断言

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func C0 added in v1.0.16

func C0(err error)

C0 checks error and panics if exists. C0 检查错误,存在则触发 panic。

func C1 added in v1.0.16

func C1[T1 comparable](v1 T1, err error) T1

C1 checks error, ensures value is not zero, and returns the value. C1 检查错误,确保值非零,并返回值。

func C2 added in v1.0.16

func C2[T1, T2 comparable](v1 T1, v2 T2, err error) (T1, T2)

C2 checks error, ensures values are not zero, and returns the values. C2 检查错误,确保值非零,并返回值。

func Done

func Done(err error)

Done panics and logs the stack trace if the error occurs, otherwise do nothing Done 当err非空时就panic且打印调用栈信息

func Fata

func Fata(err error)

Fata logs a fatal error and exits the system if the error occurs, otherwise do nothing Fata 当出错时就是调用 log.Fatal 退出系统,当不出错时就不做任何事情

func Fine

func Fine[V comparable](v V)

Fine ensures the value is not zero. Panics if the value equals its zero value. Fine 确保值非零。 如果值等于零值则触发 panic。

func Full added in v1.0.16

func Full[T any](v *T) *T

Full ensures the pointer is not nil and returns it. Panics if the pointer is nil. Full 确保指针非 nil 并返回该指针。 如果指针是 nil 则触发 panic。

func Good

func Good[V comparable](v V)

Good ensures the value is not zero. Panics if the value equals its zero value. Good 确保值非零。 如果值等于零值则触发 panic。

func Must

func Must(err error)

Must panics and logs the stack trace if the error occurs, otherwise do nothing Must 当err非空时就panic且打印调用栈信息

func Nice

func Nice[V comparable](v V) V

Nice ensures the value is not zero and returns it. Panics if the value equals its zero value. Nice 确保值非零并返回该值。 如果值等于零值则触发 panic。

func None

func None[V comparable](v V)

None ensures the value is its type's zero value. Panics if the value is not zero. None 确保值是其类型的零值。 如果值非零则触发 panic。

func Null added in v1.0.16

func Null[T any](v *T)

Null ensures the pointer is nil. Panics if the pointer is not nil. Null 确保指针是 nil。 如果指针非 nil 则触发 panic。

func P0 added in v1.0.16

func P0(err error)

P0 checks error and panics if exists. P0 检查错误,存在则触发 panic。

func P1 added in v1.0.16

func P1[T1 any](v1 *T1, err error) *T1

P1 checks error, ensures pointer is not nil, and returns the pointer. P1 检查错误,确保指针非 nil,并返回指针。

func P2 added in v1.0.16

func P2[T1, T2 any](v1 *T1, v2 *T2, err error) (*T1, *T2)

P2 checks error, ensures pointers are not nil, and returns the pointers. P2 检查错误,确保指针非 nil,并返回指针。

func Safe

func Safe[V comparable](v V)

Safe ensures the value is not zero. Panics if the value equals its zero value. Safe 确保值非零。 如果值等于零值则触发 panic。

func Soft

func Soft(err error)

Soft logs a warning and the stack trace if the error occurs, otherwise do nothing Soft 当err非空时就打印warning日志且打印调用栈信息

func Sure

func Sure[V comparable](v V) V

Sure ensures the value is not zero and returns it. Panics if the value equals its zero value. Sure 确保值非零并返回该值。 如果值等于零值则触发 panic。

func V0 added in v1.0.16

func V0(err error)

V0 checks error and panics if exists. V0 检查错误,存在则触发 panic。

func V1 added in v1.0.16

func V1[T1 any](v1 T1, err error) T1

V1 checks error and returns one value. V1 检查错误并返回一个值。

func V2 added in v1.0.16

func V2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)

V2 checks error and returns two values. V2 检查错误并返回两个值。

func Zero

func Zero[V comparable](v V)

Zero ensures the value is its type's zero value. Panics if the value is not zero (e.g., num is not 0, string is not "", pointer is not nil). Zero 确保值是其类型的零值。 如果值非零则触发 panic(例如数字非0、字符串非空、指针非nil)。

Types

type Vae

type Vae[V any] struct {
	V []V
	E error
	*Ve[[]V]
}

Vae wraps a slice value and an error with slice validation methods. Embeds Ve to provide basic error handling methods. A means slice type. Vae 封装一个切片值和一个错误,配合切片验证方法。 嵌入 Ve 来提供基础错误处理方法。A 代表切片类型。

func VAE

func VAE[V any](val []V, err error) *Vae[V]

VAE creates a Vae instance with a slice value and error. Accepts two params: one is slice and one is error interface. VAE 创建一个 Vae 实例,包含切片值和错误。 接受两个参数:切片和错误接口。

func (*Vae[V]) Fine

func (a *Vae[V]) Fine()

Fine ensures no error and slice is not empty. Fine 确保无错误且切片非空。

func (*Vae[V]) Good

func (a *Vae[V]) Good()

Good ensures no error and slice is not empty. Good 确保无错误且切片非空。

func (*Vae[V]) Have

func (a *Vae[V]) Have()

Have ensures no error and slice is not empty. Have 确保无错误且切片非空。

func (*Vae[V]) Len

func (a *Vae[V]) Len(n int)

Len ensures no error and slice has the given length. Len 确保无错误且切片长度等于给定值。

func (*Vae[V]) Length

func (a *Vae[V]) Length(n int)

Length ensures no error and slice has the given length. Length 确保无错误且切片长度等于给定值。

func (*Vae[V]) Nice

func (a *Vae[V]) Nice() []V

Nice ensures no error and slice is not empty, then returns the slice. Nice 确保无错误且切片非空,然后返回切片。

func (*Vae[V]) None

func (a *Vae[V]) None()

None ensures no error and slice is empty. None 确保无错误且切片是空的。

func (*Vae[V]) Safe

func (a *Vae[V]) Safe()

Safe ensures no error and slice is not empty. Safe 确保无错误且切片非空。

func (*Vae[V]) Size

func (a *Vae[V]) Size(n int)

Size ensures no error and slice has the given size. Size 确保无错误且切片大小等于给定值。

func (*Vae[V]) Some

func (a *Vae[V]) Some() []V

Some ensures no error and slice is not empty, then returns the slice. Some 确保无错误且切片非空,然后返回切片。

func (*Vae[V]) Sure

func (a *Vae[V]) Sure() []V

Sure ensures no error and slice is not empty, then returns the slice. Sure 确保无错误且切片非空,然后返回切片。

func (*Vae[V]) Void

func (a *Vae[V]) Void()

Void ensures no error and slice is empty. Void 确保无错误且切片是空的。

func (*Vae[V]) Zero

func (a *Vae[V]) Zero()

Zero ensures no error and slice is empty. Zero 确保无错误且切片是空的。

type Vbe

type Vbe struct {
	V bool
	E error
	*Vce[bool]
}

Vbe wraps a boolean value and an error with boolean validation methods. Embeds Vce to provide comparable validation methods. Vbe 封装一个布尔值和一个错误,配合布尔验证方法。 嵌入 Vce 来提供可比较验证方法。

func VBE

func VBE(val bool, err error) *Vbe

VBE creates a Vbe instance with a boolean value and error. VBE 创建一个 Vbe 实例,包含布尔值和错误。

func (*Vbe) FALSE

func (a *Vbe) FALSE()

FALSE ensures no error and value is false. FALSE 确保无错误且值是 false。

func (*Vbe) NO

func (a *Vbe) NO()

NO ensures no error and value is false. NO 确保无错误且值是 false。

func (*Vbe) No

func (a *Vbe) No()

No ensures no error and value is false. No 确保无错误且值是 false。

func (*Vbe) Not

func (a *Vbe) Not()

Not ensures no error and value is false. Not 确保无错误且值是 false。

func (*Vbe) OK

func (a *Vbe) OK()

OK ensures no error and value is true. OK 确保无错误且值是 true。

func (*Vbe) TRUE

func (a *Vbe) TRUE()

TRUE ensures no error and value is true. TRUE 确保无错误且值是 true。

func (*Vbe) YES

func (a *Vbe) YES()

YES ensures no error and value is true. YES 确保无错误且值是 true。

func (*Vbe) Yes

func (a *Vbe) Yes()

Yes ensures no error and value is true. Yes 确保无错误且值是 true。

type Vce

type Vce[V comparable] struct {
	V V
	E error
	*Ve[V]
}

Vce wraps a comparable value and an error with validation methods. Embeds Ve to provide basic error handling methods. Vce 封装一个可比较值和一个错误,配合验证方法。 嵌入 Ve 来提供基础错误处理方法。

func VCE

func VCE[V comparable](val V, err error) *Vce[V]

VCE creates a Vce instance with a comparable value and error. Accepts two params: one is comparable value and one is error interface. VCE 创建一个 Vce 实例,包含可比较值和错误。 接受两个参数:可比较值和错误接口。

func (*Vce[V]) Diff

func (a *Vce[V]) Diff(v V)

Diff ensures no error and value is not equal to the given value. Diff 确保无错误且值不等于给定值。

func (*Vce[V]) Different

func (a *Vce[V]) Different(v V)

Different ensures no error and value is not equal to the given value. Different 确保无错误且值不等于给定值。

func (*Vce[V]) Equals

func (a *Vce[V]) Equals(v V)

Equals ensures no error and value equals the given value. Equals 确保无错误且值等于给定值。

func (*Vce[V]) Fine

func (a *Vce[V]) Fine()

Fine ensures no error and value is not zero. Fine 确保无错误且值非零。

func (*Vce[V]) Good

func (a *Vce[V]) Good()

Good ensures no error and value is not zero. Good 确保无错误且值非零。

func (*Vce[V]) Is

func (a *Vce[V]) Is(v V)

Is ensures no error and value equals the given value. Is 确保无错误且值等于给定值。

func (*Vce[V]) Nice

func (a *Vce[V]) Nice() V

Nice ensures no error and value is not zero, then returns the value. Nice 确保无错误且值非零,然后返回值。

func (*Vce[V]) None

func (a *Vce[V]) None()

None ensures no error and value is zero. None 确保无错误且值是零值。

func (*Vce[V]) Safe

func (a *Vce[V]) Safe()

Safe ensures no error and value is not zero. Safe 确保无错误且值非零。

func (*Vce[V]) Same

func (a *Vce[V]) Same(v V)

Same ensures no error and value equals the given value. Same 确保无错误且值等于给定值。

func (*Vce[V]) Sure

func (a *Vce[V]) Sure() V

Sure ensures no error and value is not zero, then returns the value. Sure 确保无错误且值非零,然后返回值。

func (*Vce[V]) Zero

func (a *Vce[V]) Zero()

Zero ensures no error and value is zero. Zero 确保无错误且值是零值。

type Ve

type Ve[V any] struct {
	V V
	E error
}

Ve wraps a value and an error with validation methods. Ve 封装一个值和一个错误,配合验证方法。

func VE

func VE[V any](val V, err error) *Ve[V]

VE creates a Ve instance with a value and error. Accepts two params: one is any value and one is error interface. VE 创建一个 Ve 实例,包含值和错误。 接受两个参数:任意值和错误接口。

func (*Ve[V]) Done

func (a *Ve[V]) Done() V

Done checks if error exists and panics, then returns the value. The value is not checked and can be zero. Done 检查是否存在错误并触发 panic,然后返回值。 不检查值,可以是零值。

func (*Ve[V]) Must

func (a *Ve[V]) Must() V

Must checks if error exists and panics, then returns the value. The value is not checked and can be zero. Must 检查是否存在错误并触发 panic,然后返回值。 不检查值,可以是零值。

func (*Ve[V]) Omit

func (a *Ve[V]) Omit() V

Omit returns the value and ignores the error. The value can be normal, zero, or unexpected. Omit 返回值并忽视错误。 值可能是正常值、零值或异常值。

func (*Ve[V]) Skip

func (a *Ve[V]) Skip() V

Skip returns the value and ignores the error. The value can be normal, zero, or unexpected. Skip 返回值并忽视错误。 值可能是正常值、零值或异常值。

func (*Ve[V]) Soft

func (a *Ve[V]) Soft() V

Soft logs a warning if error exists, then returns the value. Returns the value without checking. Soft 如果存在错误则记录告警,然后返回值。 返回值但不检查。

type Vme

type Vme[K comparable, V any] struct {
	V map[K]V
	E error
	*Ve[map[K]V]
}

Vme wraps a map value and an error with map validation methods. Embeds Ve to provide basic error handling methods. M means map type. Vme 封装一个 map 值和一个错误,配合 map 验证方法。 嵌入 Ve 来提供基础错误处理方法。M 代表 map 类型。

func VME

func VME[K comparable, V any](val map[K]V, err error) *Vme[K, V]

VME creates a Vme instance with a map value and error. Accepts two params: one is map and one is error interface. VME 创建一个 Vme 实例,包含 map 值和错误。 接受两个参数:map 和错误接口。

func (*Vme[K, V]) Fine

func (a *Vme[K, V]) Fine()

Fine ensures no error and map is not empty. Fine 确保无错误且 map 非空。

func (*Vme[K, V]) Good

func (a *Vme[K, V]) Good()

Good ensures no error and map is not empty. Good 确保无错误且 map 非空。

func (*Vme[K, V]) Have

func (a *Vme[K, V]) Have()

Have ensures no error and map is not empty. Have 确保无错误且 map 非空。

func (*Vme[K, V]) Len

func (a *Vme[K, V]) Len(n int)

Len ensures no error and map has the given length. Len 确保无错误且 map 长度等于给定值。

func (*Vme[K, V]) Length

func (a *Vme[K, V]) Length(n int)

Length ensures no error and map has the given length. Length 确保无错误且 map 长度等于给定值。

func (*Vme[K, V]) Nice

func (a *Vme[K, V]) Nice() map[K]V

Nice ensures no error and map is not empty, then returns the map. Nice 确保无错误且 map 非空,然后返回 map。

func (*Vme[K, V]) None

func (a *Vme[K, V]) None()

None ensures no error and map is empty. None 确保无错误且 map 是空的。

func (*Vme[K, V]) Safe

func (a *Vme[K, V]) Safe()

Safe ensures no error and map is not empty. Safe 确保无错误且 map 非空。

func (*Vme[K, V]) Size

func (a *Vme[K, V]) Size(n int)

Size ensures no error and map has the given size. Size 确保无错误且 map 大小等于给定值。

func (*Vme[K, V]) Some

func (a *Vme[K, V]) Some() map[K]V

Some ensures no error and map is not empty, then returns the map. Some 确保无错误且 map 非空,然后返回 map。

func (*Vme[K, V]) Sure

func (a *Vme[K, V]) Sure() map[K]V

Sure ensures no error and map is not empty, then returns the map. Sure 确保无错误且 map 非空,然后返回 map。

func (*Vme[K, V]) Void

func (a *Vme[K, V]) Void()

Void ensures no error and map is empty. Void 确保无错误且 map 是空的。

func (*Vme[K, V]) Zero

func (a *Vme[K, V]) Zero()

Zero ensures no error and map is empty. Zero 确保无错误且 map 是空的。

type Vne

type Vne[V numType] struct {
	V V
	E error
	*Vce[V]
}

Vne wraps a numeric value and an error with numeric validation methods. Embeds Vce to provide comparable validation methods. Vne 封装一个数字值和一个错误,配合数字验证方法。 嵌入 Vce 来提供可比较验证方法。

func VNE

func VNE[V numType](val V, err error) *Vne[V]

VNE creates a Vne instance with a numeric value and error. VNE 创建一个 Vne 实例,包含数字值和错误。

func (*Vne[V]) Gt

func (a *Vne[V]) Gt(base V) V

Gt ensures no error and value is greater than base, then returns the value. Gt 确保无错误且值大于基准值,然后返回值。

func (*Vne[V]) Gte

func (a *Vne[V]) Gte(base V) V

Gte ensures no error and value is greater than or equal to base, then returns the value. Gte 确保无错误且值大于等于基准值,然后返回值。

func (*Vne[V]) Lt

func (a *Vne[V]) Lt(base V) V

Lt ensures no error and value is less than base, then returns the value. Lt 确保无错误且值小于基准值,然后返回值。

func (*Vne[V]) Lte

func (a *Vne[V]) Lte(base V) V

Lte ensures no error and value is less than or equal to base, then returns the value. Lte 确保无错误且值小于等于基准值,然后返回值。

type Vpe

type Vpe[V any] struct {
	V       *V
	E       error
	*Ve[*V] // Embed Ve to validate pointers // 嵌入 Ve 来验证指针
}

Vpe wraps a pointer value and an error with validation methods. Embeds Ve to provide basic error handling methods. Vpe 封装一个指针值和一个错误,配合验证方法。 嵌入 Ve 来提供基础错误处理方法。

func VPE

func VPE[V any](val *V, err error) *Vpe[V]

VPE creates a Vpe instance with a pointer value and error. Accepts two params: one is pointer and one is error interface. VPE 创建一个 Vpe 实例,包含指针值和错误。 接受两个参数:指针和错误接口。

func (*Vpe[V]) Fine

func (a *Vpe[V]) Fine()

Fine ensures no error and pointer is not nil. Fine 确保无错误且指针非 nil。

func (*Vpe[V]) Full added in v1.0.16

func (a *Vpe[V]) Full() *V

Full ensures no error and pointer is not nil, then returns the pointer. Full 确保无错误且指针非 nil,然后返回指针。

func (*Vpe[V]) Good

func (a *Vpe[V]) Good()

Good ensures no error and pointer is not nil. Good 确保无错误且指针非 nil。

func (*Vpe[V]) Nice

func (a *Vpe[V]) Nice() *V

Nice ensures no error and pointer is not nil, then returns the pointer. Nice 确保无错误且指针非 nil,然后返回指针。

func (*Vpe[V]) None

func (a *Vpe[V]) None()

None ensures no error and pointer is nil. None 确保无错误且指针是 nil。

func (*Vpe[V]) Null added in v1.0.16

func (a *Vpe[V]) Null()

Null ensures no error and pointer is nil. Null 确保无错误且指针是 nil。

func (*Vpe[V]) Safe

func (a *Vpe[V]) Safe()

Safe ensures no error and pointer is not nil. Safe 确保无错误且指针非 nil。

func (*Vpe[V]) Sure

func (a *Vpe[V]) Sure() *V

Sure ensures no error and pointer is not nil, then returns the pointer. Sure 确保无错误且指针非 nil,然后返回指针。

func (*Vpe[V]) Zero

func (a *Vpe[V]) Zero()

Zero ensures no error and pointer is nil. Zero 确保无错误且指针是 nil。

type Vse

type Vse struct {
	V string
	E error
	*Vce[string]
}

Vse wraps a string value and an error with string validation methods. Embeds Vce to provide comparable validation methods. Vse 封装一个字符串值和一个错误,配合字符串验证方法。 嵌入 Vce 来提供可比较验证方法。

func VSE

func VSE(val string, err error) *Vse

VSE creates a Vse instance with a string value and error. VSE 创建一个 Vse 实例,包含字符串值和错误。

func (*Vse) Contains

func (a *Vse) Contains(sub string)

Contains ensures no error and string contains the given substring. Contains 确保无错误且字符串包含给定子串。

func (*Vse) Equals

func (a *Vse) Equals(s string)

Equals ensures no error and string equals the given string. Equals 确保无错误且字符串等于给定字符串。

func (*Vse) HasPrefix

func (a *Vse) HasPrefix(prefix string)

HasPrefix ensures no error and string has the given prefix. HasPrefix 确保无错误且字符串包含给定前缀。

func (*Vse) HasSuffix

func (a *Vse) HasSuffix(suffix string)

HasSuffix ensures no error and string has the given suffix. HasSuffix 确保无错误且字符串包含给定后缀。

Image Directories

Path Synopsis
internal
demos/demo1x command
demos/demo2x command
demos/demo3x command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL