Go + Collection tutorial (4.3)

Keywords: Go gin

catalogue

Go + overview

Collection collection

Index method

Include method

Any method

All method

Filter method

Go + overview

Go + is a branch of go and data science that integrates engineering development   Scratch in the field of Python and programming teaching combines the form of python with the heart of go, so that engineers do not need to learn a new development language to process data, and beginners can learn a programming language with a lower threshold for programming and developing works.

Collection collection

Sometimes, we often need to operate on the data collection, such as selecting items that meet certain conditions, or using user-defined functions to map items to a new collection.

In most languages, we are used to using general data structures and algorithms. However, Go + does not support generics; In Go +, you can also provide some collection methods required by programs and data types.

Next, we introduce some examples of using collection methods.

Index method

Function: returns the position of the target string in the string array. If it is not found, it returns - 1.

The example code is as follows:

func Index(vs []string, t string) int {
    for i, v := range vs {
        if v == t {
            return i
        }
    }
    return -1
}

arr := ["a", "hello", "world", "b", "good", "x", "zZ", "A"]

println("String array:", arr)

println("hello Position in string array:", Index(arr, "hello"))

The results are as follows:

String array: [a hello world b good x zZ A]
hello position in string array: 1

The screenshot of the operation result is as follows:

You can also try it yourself: Please order me!  

Include method

Function: judge whether the target string is included in the slice. If it exists, return true; otherwise, return false.

The example code is as follows:

func Index(vs []string, t string) int {
    for i, v := range vs {
        if v == t {
            return i
        }
    }
    return -1
}

	
func Include(vs []string, t string) bool {
    return Index(vs, t) >= 0
}

arr := ["a", "hello", "world", "b", "good", "x", "zZ", "A"]

println("String array:", arr)

println("Target string hello Whether to include in string slice:", Include(arr, "hello"))


The results are as follows:

String array: [a hello world b good x zZ A]
Whether the target string hello is included in the string slice: true

You can also try it yourself: Please order me!

Any method

Function: if a string in the slice meets the conditions, it returns true; otherwise, it returns false.

The example code is as follows:

import (
    "strings"
)
func Any(vs []string, f func(string) bool) bool {
    for _, v := range vs {
        if f(v) {
            return true
        }
    }
    return false
}
arr := ["a", "hello", "world", "b", "good", "x", "zZ", "A"]

println("String array:", arr)

println("Does the string slice exist w Prefix:", Any(arr, func(v string) bool {
        return strings.HasPrefix(v, "w")
    }))

The results are as follows:

String array: [a hello world b good x zZ A]
Whether there is w prefix in string slice: true

You can also try it yourself: Please order me!

All method

Function: if all elements in the slice meet specific conditions, return true; otherwise, return false.

The example code is as follows:

import (
    "strings"
)
func All(vs []string, f func(string) bool) bool {
    for _, v := range vs {
        if !f(v) {
            return false
        }
    }
    return true
}
arr := ["a", "hello", "world", "b", "good", "x", "zZ", "A"]

println("String array:", arr)

println("All elements in the string slice exist w Prefix:", All(arr, func(v string) bool {
        return strings.HasPrefix(v, "w")
    }))

The results are as follows:

String array: [a hello world b good x zZ A]
All elements in the string slice have a w prefix: false

You can also try it yourself: Please order me!

Filter method

Function: returns a new slice in which all elements in the slice meet specific conditions.

The example code is as follows:

import (
    "strings"
)
func Filter(vs []string, f func(string) bool) []string {
    vsf := make([]string, 0)
    for _, v := range vs {
        if f(v) {
            vsf = append(vsf, v)
        }
    }
    return vsf
}
arr := ["a", "hello", "world", "b", "good", "x", "zZ", "A"]

println("String slice:", arr)

println("A new slice of 0 exists for all elements in the string slice:", Filter(arr, func(v string) bool {
        return strings.Contains(v, "o")
    }))

The results are as follows:

String slice: [a hello world b good x zZ A]
All elements in the string slice have a new slice of 0: [hello world good]

You can also try it yourself: Please order me!  

Posted by Qlubbie on Sat, 27 Nov 2021 11:01:15 -0800