intern

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package intern stores each distinct string once, off the Go heap, and names it with a 4-byte Symbol. Repeated text, such as field names or log labels, then costs one copy, and comparing two Symbols is comparing two integers.

Example
package main

import (
	"fmt"

	memory "github.com/stubbedev/memory-go"
	"github.com/stubbedev/memory-go/intern"
)

func main() {
	var a memory.Local
	defer a.Close()

	t, err := intern.New(&a)
	if err != nil {
		panic(err)
	}
	first, _ := t.Intern("status")
	second, _ := t.Intern("status")
	fmt.Println(first == second, t.Text(first))
}
Output:
true status

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Symbol

type Symbol uint32

Symbol names an interned string in four bytes and may be stored off-heap. The zero Symbol is nil.

type Table

type Table struct {
	// contains filtered or unexported fields
}

Table holds interned strings until Free. It is not safe for concurrent use.

func New

func New(a memory.Allocator) (*Table, error)

New returns an empty Table.

func (*Table) Free

func (t *Table) Free() error

Free releases every string. t must not be used afterwards.

func (*Table) Intern

func (t *Table) Intern(text string) (Symbol, error)

Intern returns the Symbol of text, storing a copy the first time it is seen.

func (*Table) Len

func (t *Table) Len() int

Len returns the number of distinct strings.

func (*Table) Lookup

func (t *Table) Lookup(text string) (Symbol, bool)

Lookup returns the Symbol of text, or reports false if it was never interned.

func (*Table) Text

func (t *Table) Text(s Symbol) string

Text returns the string s names. The result reads off-heap memory and is valid until Free. It panics if s is nil or from another Table with more strings.

Jump to

Keyboard shortcuts

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