pointer

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2017 License: MIT Imports: 1 Imported by: 0

README

pointer GoDoc Build Status

Go package pointer provides helpers to get pointers to values of build-in types.

go get github.com/AlekSi/pointer

API is stable. Documentation.

package motivationalexample

import (
	"encoding/json"

	"github.com/AlekSi/pointer"
)

const (
	defaultName = "some name"
)

// Stuff contains optional fields.
type Stuff struct {
	Name    *string
	Comment *string
	Value   *int64
	Time    *time.Time
}

// SomeStuff makes some JSON-encoded stuff.
func SomeStuff() (data []byte, err error) {
	return json.Marshal(&Stuff{
		Name:    pointer.ToString(defaultName),                                   // can't say &defaultName
		Comment: pointer.ToString("not yet"),                                     // can't say &"not yet"
		Value:   pointer.ToInt64(42),                                             // can't say &42 or &int64(42)
		Time:    pointer.ToTime(time.Date(2014, 6, 25, 12, 24, 40, 0, time.UTC)), // can't say &time.Date(…)
	})
}

Documentation

Overview

Package pointer provides helpers to get pointers to values of build-in types.

Example
const (
	defaultName = "some name"
)

// Stuff contains optional fields.
type Stuff struct {
	Name    *string
	Comment *string
	Value   *int64
	Time    *time.Time
}

b, _ := json.Marshal(&Stuff{
	Name:    ToString(defaultName),                                   // can't say &defaultName
	Comment: ToString("not yet"),                                     // can't say &"not yet"
	Value:   ToInt64(42),                                             // can't say &42 or &int64(42)
	Time:    ToTime(time.Date(2014, 6, 25, 12, 24, 40, 0, time.UTC)), // can't say &time.Date(…)
})

fmt.Printf("%s", b)
Output:

{"Name":"some name","Comment":"not yet","Value":42,"Time":"2014-06-25T12:24:40Z"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToBool

func ToBool(b bool) *bool

func ToByte

func ToByte(b byte) *byte

func ToComplex128

func ToComplex128(c complex128) *complex128

func ToComplex64

func ToComplex64(c complex64) *complex64

func ToError

func ToError(e error) *error

func ToFloat32

func ToFloat32(f float32) *float32

func ToFloat64

func ToFloat64(f float64) *float64

func ToInt

func ToInt(i int) *int

func ToInt16

func ToInt16(i int16) *int16

func ToInt32

func ToInt32(i int32) *int32

func ToInt64

func ToInt64(i int64) *int64

func ToInt8

func ToInt8(i int8) *int8

func ToRune

func ToRune(r rune) *rune

func ToString

func ToString(s string) *string

func ToTime

func ToTime(t time.Time) *time.Time

func ToUint

func ToUint(u uint) *uint

func ToUint16

func ToUint16(u uint16) *uint16

func ToUint32

func ToUint32(u uint32) *uint32

func ToUint64

func ToUint64(u uint64) *uint64

func ToUint8

func ToUint8(u uint8) *uint8

func ToUintptr

func ToUintptr(u uintptr) *uintptr

Types

This section is empty.

Jump to

Keyboard shortcuts

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