This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
strconv operatorsโ
This page lists all operators available in the strconv sub-package of ro.
Installโ
First, import the sub-package in your project:
go get -u github.com/samber/ro/plugins/strconv
Atoiโ
Converts strings to integers using strconv.Atoi.
import (
"fmt"
"github.com/samber/ro"
rostrconv "github.com/samber/ro/plugins/strconv"
)
obs := ro.Pipe[string, int](
ro.Just("123", "456", "789", "invalid"),
rostrconv.Atoi[string](),
)
sub := obs.Subscribe(ro.NewObserver(
func(i int) {
fmt.Printf("Next: %d\n", i)
},
func(err error) {
fmt.Printf("Error: %v\n", err)
},
func() {
fmt.Println("Completed")
},
))
defer sub.Unsubscribe()
// Next: 123
// Next: 456
// Next: 789
// Error: strconv.Atoi: parsing "invalid": invalid syntaxPrototype:func Atoi[T ~string]()ParseUint64โ
Converts strings to uint64 values with specified base and bit size.
import (
"fmt"
"github.com/samber/ro"
rostrconv "github.com/samber/ro/plugins/strconv"
)
obs := ro.Pipe[string, uint64](
ro.Just("123", "FF", "1010", "invalid"),
rostrconv.ParseUint64[string](16, 64), // Parse as hex, 64-bit unsigned
)
sub := obs.Subscribe(ro.NewObserver(
func(i uint64) {
fmt.Printf("Next: %d\n", i)
},
func(err error) {
fmt.Printf("Error: %v\n", err)
},
func() {
fmt.Println("Completed")
},
))
defer sub.Unsubscribe()
// Next: 291
// Next: 255
// Next: 4112
// Error: strconv.ParseUint: parsing "invalid": invalid syntaxPrototype:func ParseUint64[T ~string](base int, bitSize int)FormatComplexโ
Converts complex128 values to strings with specified format, precision, and bit size.
import (
"fmt"
"github.com/samber/ro"
rostrconv "github.com/samber/ro/plugins/strconv"
)
obs := ro.Pipe[complex128, string](
ro.Just(3+4i, 1+2i, 0.5+1.25i),
rostrconv.FormatComplex('f', 2, 128), // Fixed-point, 2 decimal places
)
sub := obs.Subscribe(ro.NewObserver(
func(s string) {
fmt.Printf("Next: %s\n", s)
},
func(err error) {
fmt.Printf("Error: %v\n", err)
},
func() {
fmt.Println("Completed")
},
))
defer sub.Unsubscribe()
// Next: (3.00+4.00i)
// Next: (1.00+2.00i)
// Next: (0.50+1.25i)
// CompletedSimilar:Prototype:func FormatComplex(mt byte, prec, bitSize int)ParseIntโ
Parses strings to int64 with base and bit size.
import (
"github.com/samber/ro"
rostrconv "github.com/samber/ro/plugins/strconv"
)
obs := ro.Pipe[string, int64](
ro.Just("42", "-10", "ff"),
rostrconv.ParseInt[string](10, 64),
)
sub := obs.Subscribe(ro.PrintObserver[int64]())
defer sub.Unsubscribe()
// Next: 42
// Next: -10
// Error: strconv.ParseInt: parsing "ff": invalid syntaxSimilar:Prototype:func ParseInt[T ~string](base int, bitSize int)Quoteโ
Converts strings to Go string literals using strconv.Quote.
import (
"fmt"
"github.com/samber/ro"
rostrconv "github.com/samber/ro/plugins/strconv"
)
obs := ro.Pipe[string, string](
ro.Just("hello", "world\n", "test\t\"quote\"", "path\\to\\file"),
rostrconv.Quote(),
)
sub := obs.Subscribe(ro.NewObserver(
func(s string) {
fmt.Printf("Next: %s\n", s)
},
func(err error) {
fmt.Printf("Error: %v\n", err)
},
func() {
fmt.Println("Completed")
},
))
defer sub.Unsubscribe()
// Next: "hello"
// Next: "world\n"
// Next: "test\t\"quote\""
// Next: "path\\to\\file"
// CompletedPrototype:func Quote()QuoteRuneโ
Converts runes to Go character literals using strconv.QuoteRune.
import (
"fmt"
"github.com/samber/ro"
rostrconv "github.com/samber/ro/plugins/strconv"
)
obs := ro.Pipe[rune, string](
ro.Just('a', 'b', '\n', '\t', '"'),
rostrconv.QuoteRune(),
)
sub := obs.Subscribe(ro.NewObserver(
func(s string) {
fmt.Printf("Next: %s\n", s)
},
func(err error) {
fmt.Printf("Error: %v\n", err)
},
func() {
fmt.Println("Completed")
},
))
defer sub.Unsubscribe()
// Next: 'a'
// Next: 'b'
// Next: '\n'
// Next: '\t'
// Next: '"'
// CompletedPrototype:func QuoteRune()Unquoteโ
Converts Go string literals back to strings using strconv.Unquote.
import (
"fmt"
"github.com/samber/ro"
rostrconv "github.com/samber/ro/plugins/strconv"
)
obs := ro.Pipe[string, string](
ro.Just(`"hello"`, `"world\n"`, `"test\t\"quote\""`, `"invalid\quote"`),
rostrconv.Unquote(),
)
sub := obs.Subscribe(ro.NewObserver(
func(s string) {
fmt.Printf("Next: %s\n", s)
},
func(err error) {
fmt.Printf("Error: %v\n", err)
},
func() {
fmt.Println("Completed")
},
))
defer sub.Unsubscribe()
// Next: hello
// Next: world
// Next: test "quote"
// Error: invalid syntaxPrototype:func Unquote()ParseFloatโ
Converts strings to float values with specified bit size.
import (
"fmt"
"github.com/samber/ro"
rostrconv "github.com/samber/ro/plugins/strconv"
)
obs := ro.Pipe[string, float64](
ro.Just("3.14", "2.718", "1.414", "invalid"),
rostrconv.ParseFloat[string](64),
)
sub := obs.Subscribe(ro.NewObserver(
func(f float64) {
fmt.Printf("Next: %.3f\n", f)
},
func(err error) {
fmt.Printf("Error: %v\n", err)
},
func() {
fmt.Println("Completed")
},
))
defer sub.Unsubscribe()
// Next: 3.140
// Next: 2.718
// Next: 1.414
// Error: strconv.ParseFloat: parsing "invalid": invalid syntaxPrototype:func ParseFloat[T ~string](bitSize int)ParseBoolโ
Converts strings to boolean values using strconv.ParseBool.
Accepts "1", "t", "T", "true", "TRUE", "True" for true values.
Accepts "0", "f", "F", "false", "FALSE", "False" for false values.import (
"github.com/samber/ro"
rostrconv "github.com/samber/ro/plugins/strconv"
)
obs := ro.Pipe[string, bool](
ro.Just("true", "false", "1", "0", "TRUE", "FALSE"),
rostrconv.ParseBool[string](),
)
sub := obs.Subscribe(ro.PrintObserver[bool]())
defer sub.Unsubscribe()
// Next: true
// Next: false
// Next: true
// Next: false
// Next: true
// Next: false
// CompletedSimilar:Prototype:func ParseBool[T ~string]()ParseUintโ
Converts strings to uint64 values with specified base and bit size.
The base parameter determines the number system (e.g., 10 for decimal, 16 for hexadecimal).
The bitSize parameter specifies the integer type size (e.g., 32 for uint32, 64 for uint64).import (
"github.com/samber/ro"
rostrconv "github.com/samber/ro/plugins/strconv"
)
obs := ro.Pipe[string, uint64](
ro.Just("255", "FF", "11111111", "377"),
rostrconv.ParseUint[string](16, 64), // Parse as hexadecimal, 64-bit unsigned
)
sub := obs.Subscribe(ro.PrintObserver[uint64]())
defer sub.Unsubscribe()
// Next: 255
// Next: 255
// Next: 255
// Next: 255
// Completedobs := ro.Pipe[string, uint64](
ro.Just("123", "456", "789"),
rostrconv.ParseUint[string](10, 64), // Parse as decimal, 64-bit unsigned
)
sub := obs.Subscribe(ro.PrintObserver[uint64]())
defer sub.Unsubscribe()
// Next: 123
// Next: 456
// Next: 789
// CompletedSimilar:Prototype:func ParseUint[T ~string](base int, bitSize int)FormatBoolโ
Converts boolean values to strings using strconv.FormatBool.
Returns "true" for true values and "false" for false values.
import (
"github.com/samber/ro"
rostrconv "github.com/samber/ro/plugins/strconv"
)
obs := ro.Pipe[bool, string](
ro.Just(true, false, true, false),
rostrconv.FormatBool(),
)
sub := obs.Subscribe(ro.PrintObserver[string]())
defer sub.Unsubscribe()
// Next: true
// Next: false
// Next: true
// Next: false
// CompletedSimilar:Prototype:func FormatBool()Itoaโ
Converts integers to strings using strconv.Itoa.
This is equivalent to FormatInt with base 10.
import (
"github.com/samber/ro"
rostrconv "github.com/samber/ro/plugins/strconv"
)
obs := ro.Pipe[int, string](
ro.Just(123, -456, 0, 789),
rostrconv.Itoa(),
)
sub := obs.Subscribe(ro.PrintObserver[string]())
defer sub.Unsubscribe()
// Next: 123
// Next: -456
// Next: 0
// Next: 789
// CompletedSimilar:Prototype:func Itoa()