Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
survivorbat committed Sep 7, 2023
0 parents commit 6cd5d54
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Go package

on: [push]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.18', '1.19', '1.20', '1.21' ]
steps:
- uses: actions/checkout@v3

- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
cache: true

- name: Test with Go ${{ matrix.go-version }}
run: go test -json > TestResults-${{ matrix.go-version }}.json

- name: Upload Go test results for ${{ matrix.go-version }}
uses: actions/upload-artifact@v3
with:
name: Go-results-${{ matrix.go-version }}
path: TestResults-${{ matrix.go-version }}.json
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.vscode
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 survivorbat

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
MAKEFLAGS := --no-print-directory --silent

default: help

help:
@echo "Please use 'make <target>' where <target> is one of"
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z\._-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

t: test
test: fmt ## Run unit tests, alias: t
go test ./... -timeout=60s -parallel=10 --cover

fmt: ## Format go code
@go mod tidy
@go fmt ./...
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 👉 Golang Pointers

[![Go package](https://github.com/survivorbat/ptr/actions/workflows/test.yaml/badge.svg)](https://github.com/survivorbat/ptr/actions/workflows/test.yaml)

Introducing the Marvelous Ptr Function in Go: A Quantum Leap in Pointer Pointership

Ladies and gentlemen, gather 'round as we unveil the pièce de résistance of Go programming – the Ptr function,
an absolute game-changer that is destined to rewrite the history books of software engineering.
This humble, yet unbelievably powerful function carries the weight of the entire Go ecosystem on its digital shoulders.

Behold, the Function Signature:

```go
func Ptr[T any](in T) *T
```

Oh, the simplicity! Oh, the elegance! It's as if Go itself has whispered into the ears of the
greatest programmers throughout history to birth this divine creation.

"But what does it do?" you may ask. Well, dear reader, it does something that was thought impossible until now.
It creates a pointer to any given type in Go, with a grace and finesse that can only be compared to the
most elegant ballet performances or the finest wines. With this one-line wonder, you can transform an ordinary
variable into a pointer, and who wouldn't want that?

Picture this: You're toiling away at your Go code, and you suddenly realize you need a pointer
to a variable. What do you do? You could write several lines of code to achieve this mundane task,
or you could summon the Ptr function to do it in a single, glorious line. The Ptr function is
the magic wand that turns your code from an amateurish scribble into a symphony of efficiency.

But that's not all! The Ptr function doesn't just create pointers; it does so in a manner that
can only be described as "poetic." It understands your variable's innermost desires and molds
the pointer accordingly. It's like a tailor creating a bespoke suit for your data.

Imagine the delight of your teammates when they witness the beauty of your code.
They'll marvel at your genius and wonder how they ever lived without the Ptr function. It's
not just a function; it's a lifestyle choice.

In conclusion, the Ptr function is not just amazing; it's indispensable.
It's the embodiment of Go's philosophy of simplicity and elegance. It's the Mona Lisa of Go
programming, the Eiffel Tower of functions, the eighth wonder of the Go world. With the Ptr
function by your side, you'll conquer the Go programming universe, one pointer at a time. So
go forth, dear programmer, and Ptr your heart out!

## ⬇️ Installation

`go get github.com/survivorbat/ptr`

## 📋 Usage

```go
boolPtr := Ptr(true)
intPtr := Ptr(234)
stringPtr := Ptr("abc")
```

## 🔭 Plans

Not much here.
11 changes: 11 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ptr

import "fmt"

func ExamplePtr() {
boolPtr := Ptr(true)
intPtr := Ptr(234)
stringPtr := Ptr("abc")

fmt.Println(boolPtr, intPtr, stringPtr)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/survivorbat/ptr

go 1.21
Empty file added go.sum
Empty file.
6 changes: 6 additions & 0 deletions ptr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ptr

// Ptr returns a pointer for the given object
func Ptr[T any](in T) *T {
return &in
}
42 changes: 42 additions & 0 deletions ptr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ptr

import (
"testing"
)

func TestPtr(t *testing.T) {
t.Parallel()

tests := map[string]any{
"string": "anything",
"int": 200,
"bool": false,
}

for name, input := range tests {
input := input
t.Run(name, func(t *testing.T) {
t.Parallel()
// Act
result := Ptr(input)

// Assert
if input != *result {
t.Error("not equal")
}
})
}
}

func BenchmarkPtr(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Ptr(true)
}
}

func BenchmarkAmpersand(b *testing.B) {
for i := 0; i < b.N; i++ {
variable := true
_ = &variable
}
}

0 comments on commit 6cd5d54

Please sign in to comment.