General

package
v0.2.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2024 License: MIT Imports: 3 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepCopy

func DeepCopy(value any) any

DeepCopy is a function that performs a deep copy of a given value. It takes a parameter, value, of any type and returns a new value that is a deep copy of the input value.

The function first gets the type of the input value using the reflect.TypeOf function.

If the kind of the type is not a pointer, the function performs a shallow copy of the value by simply returning the input value. This is because non-pointer values in Go are passed by value, so a new copy is created when the value is passed to the function.

If the kind of the type is a pointer, the function creates a new instance of the underlying type of the pointer using the reflect.New function. The new instance is then set to the value pointed to by the input pointer using the reflect.ValueOf function and the Set method of the reflect.Value type.

The function then returns the new value, which is a deep copy of the input value.

func ExitFromProgram

func ExitFromProgram(err error)

ExitFromProgram is a utility function that handles program termination in case of an error. It prints the error message if the error is not nil, prompts the user to press enter to exit, and then terminates the program with a status code of 1.

Parameters:

  • err: The error that caused the program termination. If err is nil, no error message is printed.

Note: This function does not return. The program is terminated by calling os.Exit(1).

func SplitIntoGroups

func SplitIntoGroups[T any](slice []T, n int) [][]T

SplitIntoGroups takes a slice of any type and an integer n as input. It splits the slice into n groups and returns a 2D slice where each inner slice represents a group. The elements are distributed based on their index in the original slice. If there are more elements than groups, the remaining elements are distributed to the groups in a round-robin fashion. If the number of groups (n) is less than or equal to 0, it panics with the message "The number of groups must be positive and non-zero". If the length of the slice is less than 2 or n is 1, it returns the original slice wrapped in a 2D slice.

Example:

slice := []int{1, 2, 3, 4, 5, 6}
n := 3
groups := SplitIntoGroups(slice, n)
fmt.Println(groups) // Output: [[1 4] [2 5] [3 6]]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL