Skip to content

proposal: strconv: Add Parse[T] #57975

Closed
Closed
@icholy

Description

@icholy

I'd like to propose a generic interface to the parsing functionality provided by strconv.

type Types interface {
	string | bool | int | int8 | int16 | int32 | int64 |
		uint | uint8 | uint16 | uint32 | uint64 | uintptr |
		float32 | float64 | complex64 | complex128
}

func Parse[T Types](s string) (T, error)

This would mainly be used from other generic functions. For example:

func QueryValue[T strconv.Types](q url.Values, key string) (T, error) {
	v := q.Get(key)
	if v == "" {
		var z T
		return z, fmt.Errorf("query value not found: %q", key)
	}
	return strconv.Parse[T](v)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions