Skip to content

gochore/pt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

30 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

pt

Go Reference Actions Codecov Go Report Card GitHub go.mod Go version GitHub tag (latest by date)

Return pointer of basic type value.

Example

package main

import "github.com/gochore/pt"

func main() {
	// πŸ’€ It cannot work because Go does not allow taking the address of a constant or literal.
	f(&100)

	// πŸ˜• It works, but it requires two lines and declares a variable that could pollute the namespace.
	v := 100
	f(&v)

	// 😊 It works. Only one line and no new variables.
	// But you have to use different functions for different types.
	// It's the only way to do it before Go1.18.
	f(pt.Int(100))

	// 🀩 It works. Only one line and no new variables, and a single function for all types.
	// It's based on generics, so it requires Go1.18 and above.
	f(pt.P(100))
}

func f(p *int) {
	// πŸ’€ It could panic if p is nil.
	println(*p)

	// πŸ˜• It's safe, but it requires multiple lines and declares a variable that could pollute the namespace.
	v := 0
	if p != nil {
		v = *p
	}
	println(v)

	// 🀩 It's safe. Only one line and no new variables.
	// It's based on generics, so it requires Go1.18 and above.
	println(pt.V(p))
}

Document

Go1.18 and later

func P

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

P returns pointer of v. It's a short form of "Pointer" or "GetPointer".

func V

func V[T any](p *T) T

V returns value of p. If p is nil, return zero value of T. It's a short form of "Value" or "GetValue".

Before Go1.18 (deprecated)

func Bool

func Bool(v bool) *bool

Bool returns pointer of bool

func Byte

func Byte(v byte) *byte

Byte returns pointer of byte

func Complex128

func Complex128(v complex128) *complex128

Complex128 returns pointer of complex128

func Complex64

func Complex64(v complex64) *complex64

Complex64 returns pointer of complex64

func Duration

func Duration(v time.Duration) *time.Duration

Duration returns pointer of time.Duration

func Float32

func Float32(v float32) *float32

Float32 returns pointer of float32

func Float64

func Float64(v float64) *float64

Float64 returns pointer of float64

func Int

func Int(v int) *int

Int returns pointer of int

func Int16

func Int16(v int16) *int16

Int16 returns pointer of int16

func Int32

func Int32(v int32) *int32

Int32 returns pointer of int32

func Int64

func Int64(v int64) *int64

Int64 returns pointer of int64

func Int8

func Int8(v int8) *int8

Int8 returns pointer of int8

func Rune

func Rune(v rune) *rune

Rune returns pointer of rune

func String

func String(v string) *string

String returns pointer of string

func Time

func Time(v time.Time) *time.Time

Time returns pointer of time.Time

func Uint

func Uint(v uint) *uint

Uint returns pointer of uint

func Uint16

func Uint16(v uint16) *uint16

Uint16 returns pointer of uint16

func Uint32

func Uint32(v uint32) *uint32

Uint32 returns pointer of uint32

func Uint64

func Uint64(v uint64) *uint64

Uint64 returns pointer of uint64

func Uint8

func Uint8(v uint8) *uint8

Uint8 returns pointer of uint8

func Uintptr

func Uintptr(v uintptr) *uintptr

Uintptr returns pointer of uintptr