ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - PumpkinSeed/structs: Golang struct operations.
Golang struct operations. Contribute to PumpkinSeed/structs development by creating an account on GitHub.
Visit Site

GitHub - PumpkinSeed/structs: Golang struct operations.

GitHub - PumpkinSeed/structs: Golang struct operations.

Golang structs

Package structs implements simple functions to manipulate structs in Golang.

Documentation Go Report Card license Build Status Awesome

Get it

go get github.com/PumpkinSeed/structs

Contains

Contains reports whether value is within struct

package main

import "github.com/PumpkinSeed/structs"

type Tst struct {
    TestString  string
    TestFloat32 float32
    TestFloat64 float64
}

func main() {
    tst := Tst{
        TestString:  "test",
        TestFloat32: 13.444,
        TestFloat64: 16.444,
    }

    result := structs.Contains(tst, float64(16.444)) // true
    result = structs.Contains(tst, float32(13.444)) // true
}

Benchmark

BenchmarkContains-4   	 3000000	       492 ns/op

Compare

Compare returns a boolean comparing two struct

package main

import "github.com/PumpkinSeed/structs"

type TstA struct {
	TestInt   int
	TestInt8  int8
	TestInt16 int16
}

type TstB struct {
	TestInt   int
	TestInt8  int8
	TestInt16 int16
}

func main() {
    tstA := TstA{
        TestInt:   12,
	    TestInt8:  42,
	    TestInt16: 55,
    }

    tstB := TstB{
        TestInt:   12,
	    TestInt8:  42,
	    TestInt16: 55,
    }

    result := structs.Compare(testStructA, testStructB) // true
}


Benchmark

BenchmarkCompareEqual-4      	 5000000	       379 ns/op
BenchmarkCompareNotEqual-4   	 5000000	       372 ns/op

Index

Index returns the index of the first instance of the value in struct

package main

import "github.com/PumpkinSeed/structs"

type Tst struct {
    TestInt     int
	TestInt8    int8
	TestInt16   int16
	TestInt32   int32
	TestInt64   int64
	TestString  string
	TestBool    bool
	TestFloat32 float32
	TestFloat64 float64
}

func main() {
    tst := Tst{
        TestInt:     12,
		TestInt8:    42,
		TestInt16:   55,
		TestInt32:   33,
		TestInt64:   78,
		TestString:  "test",
		TestBool:    false,
		TestFloat32: 13.444,
		TestFloat64: 16.444,
    }

    result := structs.Index(testStruct, "test") // 5
}

Benchmark

BenchmarkIndex-4             	 5000000	       242 ns/op

FieldNameByValue

FieldNameByValue returns the field's name of the first instance of the value in struct

package main

import "github.com/PumpkinSeed/structs"

type Tst struct {
    TestInt     int
	TestInt8    int8
	TestInt16   int16
	TestInt32   int32
	TestInt64   int64
	TestString  string
	TestBool    bool
	TestFloat32 float32
	TestFloat64 float64
}

func main() {
    tst := Tst{
        TestInt:     12,
		TestInt8:    42,
		TestInt16:   55,
		TestInt32:   33,
		TestInt64:   78,
		TestString:  "test",
		TestBool:    false,
		TestFloat32: 13.444,
		TestFloat64: 16.444,
    }

    result := structs.FieldNameByValue(testStruct, "test") // TestString
}

Benchmark

BenchmarkFieldNameByValue-4   	 5000000	       293 ns/op

Map

The second parameter is a function, apply the function on each field on the struct, or on the condition determined in the third argument

package main

import "github.com/PumpkinSeed/structs"

type Tst struct {
	Username string
	Title    string
	Content  string
}

func main() {
    tst := Tst{
		Username: "PumpkinSeed",
		Title:    "Test title",
		Content:  "Test content",
	}

    result, err := structs.Map(&ts, func(v reflect.Value) error {
		if v.Type() == stringType {
			v.SetString(strings.ToLower(v.String()))
		}
		return nil
	})
}

Benchmark

BenchmarkMap-4                	 5000000	       268 ns/op

Replace

Replace returns a copy of the struct with the first non-overlapping instance of old replaced by new, the last param (n) is the limit, if n < 0, there is no limit on the number of replacements

package main

import "github.com/PumpkinSeed/structs"

type Tst struct {
    TestInt     int
	TestInt8    int8
	TestInt16   int16
	TestInt32   int32
	TestInt64   int64
	TestString1    string
	TestString2    string
	TestString3    string
	TestString4    string
	TestBool    bool
	TestFloat32 float32
	TestFloat64 float64
}

func main() {
    tst := Tst{
        TestInt:     12,
		TestInt8:    42,
		TestInt16:   55,
		TestInt32:   33,
		TestInt64:   78,
		TestString1:    "test",
		TestString2:    "test",
		TestString3:    "test",
		TestString4:    "test",
		TestBool:    false,
		TestFloat32: 13.444,
		TestFloat64: 16.444,
    }

    result, err := structs.Replace(&ts, "test", "new", 2)
}

Benchmark

BenchmarkReplace-4            	 2000000	       655 ns/op

ToDo

  • Upgrade GoDoc
  • Implement Map

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory