gobutil

package module
v0.0.0-...-0721bb4 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: MIT Imports: 5 Imported by: 0

README

codecov Build Status go.dev Go Report Card Licenses

gobutil

golang gob utils for common use

Basic Usage

Installation

To get the package, execute:

go get github.com/gofika/gobutil
Example
package main

import (
	"fmt"

	"github.com/gofika/gobutil"
)

func main() {
	type Foo struct {
		Name  string
		Value int
	}
	type Bar struct {
		Name  string
		Value int
	}
	foo := &Foo{"Jason", 100}
	// deep copy for different struct
	bar, err := gobutil.DeepCopy[Bar](foo)
	if err != nil {
		fmt.Printf("DeepCopy failed. err: %s\n", err.Error())
		return
	}
	fmt.Printf("bar.Name: %s\n", bar.Name)
	fmt.Printf("bar.Value: %d\n", bar.Value)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepCopy

func DeepCopy[T any](src any) (T, error)

DeepCopy clone object. can clone without same type

Example:

type Foo struct {
    Name string
    Value int
}

type Bar struct {
    Name string
    Value int
}

foo := &Foo { "Jason", 100}
bar, err := DeepCopy[Bar](foo)
fmt.Printf("%+v\n", bar)

func DeepCopyAny

func DeepCopyAny(dst, src any) error

DeepCopyAny clone object to dst. can clone without same type

func ReadBytes

func ReadBytes[T any](b []byte) (T, error)

ReadBytes read typed struct from bytes

func ReadBytesAny

func ReadBytesAny(b []byte, v any) error

ReadBytesAny read struct from bytes

func ReadFile

func ReadFile[T any](filename string) (T, error)

ReadFile read struct from gob stream file

func ReadFileAny

func ReadFileAny(filename string, v any) error

ReadFileAny read struct from gob stream file

func ReadReader

func ReadReader[T any](r io.Reader) (T, error)

ReadReader read struct from reader

func ReadReaderAny

func ReadReaderAny(r io.Reader, v any) error

ReadReaderAny read struct from reader

func WriteBytes

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

WriteBytes write struct to gob stream bytes

func WriteFile

func WriteFile(filename string, v any) error

WriteFile write struct to gob stream file

func WriteWriter

func WriteWriter(w io.Writer, v any) error

WriteWriter write struct to gob stream writer

Types

This section is empty.

Jump to

Keyboard shortcuts

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