sudo

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2021 License: MIT Imports: 2 Imported by: 2

README

Sudo

go.dev Go Report Card SourceGraph

sudo is a package to make reflect more powerful (and dangerous).

It exports a single function, Sudo, which when passed a reflect.Value, will return a new reflect.Value with the read-only restrictions removed.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sudo

func Sudo(v reflect.Value) reflect.Value

Sudo takes a reflect.Value where CanSet is false, and turns it into one where CanSet is true.

Example
package main

import (
	"fmt"
	"reflect"

	"github.com/zeebo/sudo"
)

func main() {
	var s struct{ x int }
	x := reflect.ValueOf(&s).Elem().FieldByName("x")

	// Because x went through an unexported field, we can't set it.
	fmt.Println(x.CanSet(), s.x)

	// But if we Sudo the reflect.Value
	x = sudo.Sudo(x)

	// then our wildest dreams will come true.
	x.SetInt(10)
	fmt.Println(x.CanSet(), s.x)

}
Output:
false 0
true 10

Types

This section is empty.

Jump to

Keyboard shortcuts

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