Go File

2022/10/20

json

  
import (
	"encoding/json"
	"os"
)
func GetJSONContent(filename string, content interface{}) error {
	bytes, err := os.ReadFile(filename)
	if err != nil {
		return err
	}
	return json.Unmarshal(bytes, content)
}

excel

  

import (
	"fmt"
	"strings"
	"github.com/xuri/excelize/v2"
)
func GetExcelContent(filename string) ([][]string, error) {
	f, err := excelize.OpenFile(filename)
	if err != nil {
		return nil, err
	}
	defer func() {
		// Close the spreadsheet.
		if err2 := f.Close(); err2 != nil {
			fmt.Println(err2)
		}
	}()
	// Get all the rows in a sheet.
	rows, err := f.GetRows("sheet1")
	for _, v := range rows {
		for kk, vv := range v {
			// 过滤非法字符
			v[kk] = strings.Replace(vv, "\t", "", -1)
		}
	}
	return rows, err
}

Search

    Table of Contents