diff --git a/README.md b/README.md index 9f8ea94..a2553fd 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # sugar -Syntactic Sugar for Golang \ No newline at end of file +## Syntactic Sugar for Golang + +This package provides some convenience functions that help keep code clean. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..099ab01 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitlab.com/CRThaze/sugar + +go 1.17 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/sugar.go b/sugar.go new file mode 100644 index 0000000..99911bb --- /dev/null +++ b/sugar.go @@ -0,0 +1,123 @@ +// Package sugar +package sugar + +import "fmt" + +/* + * Literal Addressers + */ + +// int +func IntPtr(v int) *int { + return &v +} + +// int8 +func Int8Ptr(v int8) *int8 { + return &v +} + +// int16 +func Int16Ptr(v int16) *int16 { + return &v +} + +// int32 +func Int32Ptr(v int32) *int32 { + return &v +} + +// rune +func RunePtr(v rune) *rune { + return &v +} + +// int64 +func Int64Ptr(v int64) *int64 { + return &v +} + +// uint +func UintPtr(v uint) *uint { + return &v +} + +// uint8 +func Uint8Ptr(v uint8) *uint8 { + return &v +} + +// byte +func BytePtr(v byte) *byte { + return &v +} + +// uint16 +func Uint16Ptr(v uint16) *uint16 { + return &v +} + +// uint32 +func Uint32Ptr(v uint32) *uint32 { + return &v +} + +// uint64 +func Uint64Ptr(v uint64) *uint64 { + return &v +} + +// float32 +func Float32Ptr(v float32) *float32 { + return &v +} + +// float64 +func Float64Ptr(v float64) *float64 { + return &v +} + +// complex64 +func Complex64Ptr(v complex64) *complex64 { + return &v +} + +// complex128 +func Complex128Ptr(v complex128) *complex128 { + return &v +} + +// string +func StringPtr(v string) *string { + return &v +} +func StrPtr(v string) *string { + return &v +} + +// bool +func BoolPtr(v bool) *bool { + return &v +} + +/* + * Error Handling + */ + +func Check(err error) { + if err != nil { + fmt.Printf("%+v", err) + } +} + +func CheckLog(err error, fn func(format string, a ...interface{})) { + if err != nil { + fn("%+v", err) + } +} + +func CheckPanic(err error) { + if err != nil { + panic(err) + } +}