Skip to content

Commit

Permalink
Support pt.V (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfogre authored Oct 24, 2023
1 parent 5594286 commit 4bb4907
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 147 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Test

on:
push:
branches: [ "master" ]
tags: [ "v*" ]
pull_request:
branches: [ "master" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.18'

- name: Format
run: gofmt -l . && test -z "$(gofmt -l .)"

- name: Build
run: go build -v ./...

- name: Test
run: go test -race -coverprofile=coverage.out -covermode=atomic

- name: Upload coverage
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}

- name: Vet
run: go vet -v ./...

- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
15 changes: 0 additions & 15 deletions .travis.yml

This file was deleted.

133 changes: 79 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# pt

[![Build Status](https://travis-ci.com/gochore/pt.svg?branch=master)](https://travis-ci.com/gochore/pt)
[![codecov](https://codecov.io/gh/gochore/pt/branch/master/graph/badge.svg)](https://codecov.io/gh/gochore/pt)
[![Go Reference](https://pkg.go.dev/badge/github.com/gochore/pt.svg)](https://pkg.go.dev/github.com/gochore/pt)
[![Actions](https://github.com/gochore/pt/actions/workflows/test.yaml/badge.svg)](https://github.com/gochore/pt/actions)
[![Codecov](https://codecov.io/gh/gochore/pt/branch/master/graph/badge.svg)](https://codecov.io/gh/gochore/pt)
[![Go Report Card](https://goreportcard.com/badge/github.com/gochore/pt)](https://goreportcard.com/report/github.com/gochore/pt)
[![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/gochore/pt)](https://github.com/gochore/pt/blob/master/go.mod)
[![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/gochore/pt)](https://github.com/gochore/pt/releases)
Expand All @@ -16,181 +17,205 @@ package main
import "github.com/gochore/pt"

func main() {
// wrong
f(&100) // can not compile
// 💀 It cannot work because Go does not allow taking the address of a constant or literal.
f(&100)

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

// good
// 😊 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))

// good, with generics, need go1.18+
// 🤩 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(*int) {
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 above
### Go1.18 and later

#### func P

```go
func P[V any](v V) *V
func P[T any](v T) *T
```
P returns pointer of v.
It's a short form of "Pointer" or "GetPointer".

#### func V

```go
func V[T any](p *T) T
```
P return pointer of v
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
### Before Go1.18 (deprecated)

#### func Bool
#### func Bool

```go
func Bool(v bool) *bool
```
Bool return pointer of bool
Bool returns pointer of bool

#### func Byte
#### func Byte

```go
func Byte(v byte) *byte
```
Byte return pointer of byte
Byte returns pointer of byte

#### func Complex128
#### func Complex128

```go
func Complex128(v complex128) *complex128
```
Complex128 return pointer of complex128
Complex128 returns pointer of complex128

#### func Complex64
#### func Complex64

```go
func Complex64(v complex64) *complex64
```
Complex64 return pointer of complex64
Complex64 returns pointer of complex64

#### func Duration
#### func Duration

```go
func Duration(v time.Duration) *time.Duration
```
Duration return pointer of time.Duration
Duration returns pointer of time.Duration

#### func Float32
#### func Float32

```go
func Float32(v float32) *float32
```
Float32 return pointer of float32
Float32 returns pointer of float32

#### func Float64
#### func Float64

```go
func Float64(v float64) *float64
```
Float64 return pointer of float64
Float64 returns pointer of float64

#### func Int
#### func Int

```go
func Int(v int) *int
```
Int return pointer of int
Int returns pointer of int

#### func Int16
#### func Int16

```go
func Int16(v int16) *int16
```
Int16 return pointer of int16
Int16 returns pointer of int16

#### func Int32
#### func Int32

```go
func Int32(v int32) *int32
```
Int32 return pointer of int32
Int32 returns pointer of int32

#### func Int64
#### func Int64

```go
func Int64(v int64) *int64
```
Int64 return pointer of int64
Int64 returns pointer of int64

#### func Int8
#### func Int8

```go
func Int8(v int8) *int8
```
Int8 return pointer of int8
Int8 returns pointer of int8

#### func Rune
#### func Rune

```go
func Rune(v rune) *rune
```
Rune return pointer of rune
Rune returns pointer of rune

#### func String
#### func String

```go
func String(v string) *string
```
String return pointer of string
String returns pointer of string

#### func Time
#### func Time

```go
func Time(v time.Time) *time.Time
```
Time return pointer of time.Time
Time returns pointer of time.Time

#### func Uint
#### func Uint

```go
func Uint(v uint) *uint
```
Uint return pointer of uint
Uint returns pointer of uint

#### func Uint16
#### func Uint16

```go
func Uint16(v uint16) *uint16
```
Uint16 return pointer of uint16
Uint16 returns pointer of uint16

#### func Uint32
#### func Uint32

```go
func Uint32(v uint32) *uint32
```
Uint32 return pointer of uint32
Uint32 returns pointer of uint32

#### func Uint64
#### func Uint64

```go
func Uint64(v uint64) *uint64
```
Uint64 return pointer of uint64
Uint64 returns pointer of uint64

#### func Uint8
#### func Uint8

```go
func Uint8(v uint8) *uint8
```
Uint8 return pointer of uint8
Uint8 returns pointer of uint8

#### func Uintptr
#### func Uintptr

```go
func Uintptr(v uintptr) *uintptr
```
Uintptr return pointer of uintptr
Uintptr returns pointer of uintptr
22 changes: 0 additions & 22 deletions _example/main.go

This file was deleted.

Loading

0 comments on commit 4bb4907

Please sign in to comment.