G-array is a GoLang library, that contains the generic function to do the array operations.

G-array

Garray is a library written in Go (Golang). Which have a collection of functions to do the array operations.

Installation

To install G-array package

go get -u github.com/99x/garray

import the package

import "github.com/99x/garray"

Usage

The following test data array is used for all the examples of this article.

Sample data array

[
  {
    "guid": "58deff39-7366-4edf-8a7f-482bcd9e9bb7",
    "isActive": true,
    "age": 24,
    "name": "Maryanne Case",
    "gender": "female"
  },
  {
    "guid": "5d65f406-6007-4695-a9eb-73425edc4fea",
    "isActive": false,
    "age": 51,
    "name": "Christensen Acosta",
    "gender": "male"
  },
  {
    "guid": "45cc3685-bf2f-48a3-93eb-7351325eef43",
    "isActive": true,
    "age": 50,
    "name": "Shelton Brooks",
    "gender": "male"
  },
  {
    "guid": "99055545-b8c9-4d16-9cef-b0cf0527a3e9",
    "isActive": false,
    "age": 79,
    "name": "Bass Riley",
    "gender": "male"
  },
  {
    "guid": "de082e93-24a4-4219-81b8-6013097848d9",
    "isActive": true,
    "age": 52,
    "name": "Eddie Vaughan",
    "gender": "female"
  },
  {
    "guid": "24f4ca25-9485-443e-bc20-2c48f977bcef",
    "isActive": false,
    "age": 50,
    "name": "Krista Duncan",
    "gender": "female"
  }
]

This is the struct for the data deserialize

type User struct {
   Guid     string `json:"guid"`
   IsActive bool   `json:"isActive"`
   Age      int    `json:"age"`
   Name     string `json:"name"`
   Gender   string `json:"gender"`
}

Find

The Find() function returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, nill is returned.

Example:

func main() {
   found := *garray.Find(users, func(user User) bool {
      return user.Age > 50
   })
   log.Println(found)
}

As the result of the return function, it will return the following result

{
  "guid": "5d65f406-6007-4695-a9eb-73425edc4fea",
  "isActive": false,
  "age": 51,
  "name": "Christensen Acosta",
  "gender": "male"
}

FindIndex

The FindIndex() function returns the index of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, -1 is returned.

Example 1:

func main() {

   findIndex := garray.FindIndex(users, func(user User) bool {
      return user.Age > 50
   })
   log.Println(findIndex) // result: 1
}

Example 2:

func main() {

   findIndex := garray.FindIndex(users, func(user User) bool {
      return user.Age > 100
   })
   log.Println(findIndex) // result: -1
}

Search

The Search() function returns the array of elements from the provided array that satisfies the provided testing function. If no values satisfy the testing function, an ***empty array ***is returned.

Example:

func main() {

   log.Println(users)
   log.Printf("Count : %d", len(users)) // output: 6
   searchData := garray.Search(users, func(user User, _ int) bool {
      return user.Age > 50
   })
   log.Println(searchData)
   log.Printf("Count : %d", len(searchData)) // output: 3
}

Remove

The Remove() function returns the given array excluding the element of the given index

Example

func main() {

   log.Printf("Count: %d", len(users)) // Count: 6
   log.Printf("1st user's name: %s", users[0].Name) // 1st user's name: Maryanne Case
   log.Printf("2nd user's name: %s", users[1].Name) // 2nd user's name: Christensen Acosta

   users = garray.Remove(users, 0)
   log.Println("First element removed")

   log.Printf("Count: %d", len(users)) //  Count: 5
   log.Printf("1st user's name: %s", users[0].Name) // 1st user's name: Christensen Acosta
}

Splice

The Splice() function is the same as Remove() function. Additionally, we can pass delete count as the 3rd parameter.

func main() {

   log.Printf("Count: %d", len(users)) // Count: 6
   log.Printf("1st user's name: %s", users[0].Name) // 1st user's name: Maryanne Case
   log.Printf("5th user's name: %s", users[4].Name) // 5th user's name: Eddie Vaughan

   users = garray.Splice(users, 1, 3) 
   log.Printf("Count: %d", len(users)) //  Count: 3
   log.Printf("1st user's name: %s", users[0].Name) // 1st user's name: Maryanne Case
   log.Printf("2nd user's name: %s", users[1].Name) // 2nd user's name: Eddie Vaughan
}

Filter

The Filter() function returns an array excluding all elements that pass the test implemented by the provided function.

func main() {

   ReadUsersData()
   log.Printf("Count: %d", len(users)) // Count: 6

   users = garray.Filter(users, func(user User, _ int) bool {
      return user.Age > 50
   })
   log.Println("Array filtered if age > 50")
   log.Printf("Count: %d", len(users)) // Count: 3
}

Map

The Map() function returns an array populated with the results of calling a provided function on every element in the calling array.

func main() {

   ReadUsersData()
   log.Println(users)
   names := *garray.Map(users, func(users User, _ int) string {
      return users.Name
   })

   log.Println(names) // [Maryanne Case Christensen Acosta Shelton Brooks Bass Riley Eddie Vaughan Krista Duncan]
}
Owner
99x
Headquartered in Sri Lanka, 99x is a technology company co-creating well-engineered, innovative digital products for the Scandinavian market.
99x
Similar Resources

The library parse the system netrc file for golang

gonetrc This is the library parse the system netrc file, support linux/macos/win

Jul 8, 2022

Simple and fast webp library for golang

Simple and fast webp library for golang

go-webp Golang Webp library for encoding and decoding, using C binding for Google libwebp Requirements libwebp Benchmarks % go test -bench "^Benchmark

Dec 28, 2022

Opensea-go - Golang's library for OpenSea APIs

opensea-go Golang's library for OpenSea APIs (https://docs.opensea.io/reference)

Nov 26, 2022

Simple RESTful API for WhatsApp in Golang (using the Whatsmeow multi device library)

Simple RESTful API for WhatsApp in Golang (using the Whatsmeow multi device library)

WUZAPI WuzAPI is an implementation of @tulir/whatsmeow library as a simple RESTful API service with multiple device support and concurrent sessions. W

Dec 30, 2022

Desenvolvendo-Sistema-Planejamento-Financeiro-GoLang - Developing a Financial Planning System with Golang

dio-expert-session-finance Pré Desenvolvimento Vamos criar um projeto no Github

Jan 27, 2022

Client-server-golang-sqs - Client Server with SQS and golang

Client Server with SQS and golang Multi-threaded client-server demo with Go What

Feb 14, 2022

Golang-action - A template repository for writing custom GitHub Actions in Golang

Golang Action A template repository for writing custom GitHub Actions in Golang.

Feb 12, 2022

Go Client Library for Amazon Product Advertising API

go-amazon-product-advertising-api Go Client Library for Amazon Product Advertising API How to Use go get -u github.com/ngs/go-amazon-product-advertisi

Sep 27, 2022

A Go client library for the Twitter 1.1 API

Anaconda Anaconda is a simple, transparent Go package for accessing version 1.1 of the Twitter API. Successful API queries return native Go structs th

Jan 1, 2023
A Go client library enabling programs to perform CRUD operations on the goharbor API.

goharbor-client A Go client library enabling programs to perform CRUD operations on the goharbor API. This client library utilizes types generated by

Jan 10, 2022
Api-product - A basic REST-ish API that allows you to perform CRUD operations for Products

Description A basic REST-ish API that allows you to perform CRUD operations for

Jan 3, 2022
Lambda service function

openfaas-lambda These are sets of example that i've used when migrating aws lambda to openfaas platform. A part of documenting on how it works, I'm op

Oct 26, 2022
A Lambda function built with SAM (Serverless Application Module)

AWS SAM Lambda Function © Israel Pereira Tavares da Silva The AWS Serverless Application Model (SAM) is an open-source framework for building serverle

Dec 19, 2021
An experimental OpenAPI -> Terraform Provider generator that does not yet function

tfpgen An experimental OpenAPI -> Terraform Provider generator that does not yet function. The goal is to allow developers to incrementally generate a

Feb 19, 2022
a Go (Golang) MusicBrainz WS2 client library - work in progress
a Go (Golang) MusicBrainz WS2 client library - work in progress

gomusicbrainz a Go (Golang) MusicBrainz WS2 client library - a work in progress. Current state Currently GoMusicBrainz provides methods to perform sea

Sep 28, 2022
Simple no frills AWS S3 Golang Library using REST with V4 Signing (without AWS Go SDK)

simples3 : Simple no frills AWS S3 Library using REST with V4 Signing Overview SimpleS3 is a golang library for uploading and deleting objects on S3 b

Nov 4, 2022
golang library for textbelt.com

textbelt.com API for GO This library sends text messages to mobile phones using http://textbelt.com Installing go get $ go get gopkg.in/dietsche/textb

Nov 15, 2022
Our library to use the sendcloud API endpoints in golang.

gosendcloud With this library it should be possible to interact with the endpoints of the sendcloud API via golang functions. Since we can not impleme

Dec 3, 2021
A Golang Client Library for building Cosmos SDK chain clients

Cosmos Client Lib in Go This is the start of ideas around how to implement the cosmos client libraries in a seperate repo How to instantiate and use t

Jan 6, 2023