encoding

package
v0.0.0-...-3bb151c Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: MIT Imports: 8 Imported by: 4

Documentation

Overview

Package encoding 提供围绕 encoding.TextMarshaler 和 encoding.TextUnmarshaler 的通用文本编解码辅助。

常见入口:

  • 用 MarshalText 统一把常见标量、[]byte 或实现了 encoding.TextMarshaler 的值编码为文本。
  • 用 UnmarshalText 把文本解码回目标值,支持指针和 reflect.Value。

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalText

func MarshalText(v any) ([]byte, error)

MarshalText 将常见标量、[]byte 或实现 encoding.TextMarshaler 的值编码为文本。

Example
package main

import (
	"fmt"

	"github.com/octohelm/x/encoding"
)

func main() {
	data, err := encoding.MarshalText([]byte("hi"))
	if err != nil {
		panic(err)
	}

	fmt.Println(string(data))
}
Output:
aGk=

func UnmarshalText

func UnmarshalText(v any, data []byte) error

UnmarshalText 将文本解码到目标值,支持指针、reflect.Value 和 encoding.TextUnmarshaler。

Example
package main

import (
	"fmt"

	"github.com/octohelm/x/encoding"
)

func main() {
	var text string
	if err := encoding.UnmarshalText(&text, []byte("hello")); err != nil {
		panic(err)
	}

	fmt.Println(text)
}
Output:
hello

Types

This section is empty.

Jump to

Keyboard shortcuts

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