jsonutil

package module
v0.0.0-...-9dc08cd Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2024 License: MIT Imports: 3 Imported by: 0

README

codecov Build Status go.dev Go Report Card Licenses

jsonutil

golang json utils for common use

Basic Usage

Installation

To get the package, execute:

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

import (
	"fmt"
	"github.com/gofika/jsonutil"
)

type Foo struct {
	Name  string `json:"name"`
	Value int    `json:"value"`
}

func main() {
	foo := &Foo{
		Name:  "Jason",
		Value: 100,
	}
	name := "foo.json"
	// write struct to file
	err := jsonutil.WriteFile(name, foo)
	if err != nil {
		fmt.Printf("WriteFile failed. err: %s\n", err.Error())
		return
	}
	// read struct from file
	bar, err = jsonutil.ReadFile[Foo](name)
	if err != nil {
		fmt.Printf("ReadFile 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 ReadFile

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

ReadFile read struct from json file

func ReadFileAny

func ReadFileAny(filename string, v any) error

ReadFileAny read struct from json file

func WriteFile

func WriteFile(filename string, v any) error

WriteFile write struct to json file

func WriteFileIndent

func WriteFileIndent(filename string, v any, indent string) error

WriteFileIndent write struct to json file with indent

Types

This section is empty.

Jump to

Keyboard shortcuts

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