cast

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package cast provides helpful functions for casting. It includes casting between different (castable)types for slice.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As[R any, T any](from []T) ([]R, bool)

As creates new slice instance and casts from elements to result slice. It reports true when from slice is empty.

Example
package main

import (
	"fmt"

	"github.com/Prastiwar/Go-flow/reflection/cast"
)

func main() {
	arr := []interface{}{"1", "2", "3"}

	stringArr, ok := cast.As[string](arr)
	if !ok {
		panic("cannot cast between provided two types")
	}
	fmt.Println(stringArr)

}
Output:

[1 2 3]

func Parse added in v0.12.0

func Parse[R any, T any](from []T) ([]R, bool)

Parse creates new slice instance and casts, converts or parses from elements to result slice. It reports true when from slice is empty or false if there was any error during parsing.

NOTE: conversion from untyped int to string yields a string of one rune, not a string of digits.

Example
package main

import (
	"fmt"

	"github.com/Prastiwar/Go-flow/reflection/cast"
)

func main() {
	arr := []interface{}{"1", "2", "3"}

	stringArr, ok := cast.Parse[int32](arr)
	if !ok {
		panic("cannot parse between provided two types")
	}
	fmt.Println(stringArr)

}
Output:

[1 2 3]

Types

This section is empty.

Jump to

Keyboard shortcuts

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