utils

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2023 License: Apache-2.0 Imports: 25 Imported by: 0

README

utils

some go-utils

Documentation

Overview

Package utils provides some efficient functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertExcel added in v0.0.3

func ConvertExcel(filePath, ext string, delimiter rune) error

ConvertExcelToTSV converts the Excel file to the text file by delimiter.

func ConvertExcelToCSV

func ConvertExcelToCSV(filePath string) error

ConvertExcelToCSV converts the Excel file to the CSV format file.

func ConvertExcelToTSV

func ConvertExcelToTSV(filePath string) error

ConvertExcelToTSV converts the Excel file to the TSV format file.

func ConvertStringToCharByte added in v0.0.2

func ConvertStringToCharByte(s string) ([]byte, error)

ConvertStringToCharByte converts the given string(char) to a byte slice, if error returns nil.

func ConvertStringToCharRune added in v0.0.2

func ConvertStringToCharRune(s string) (rune, error)

ConvertStringToCharRune converts the given string(char) to rune, if error returns 0 and error.

func CopyByReader

func CopyByReader(src io.Reader, dst io.Writer, buffer ...[]byte) error

CopyByReader copies the src Reader to the dst Writer.

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies the src file to the dst file.

func HasNullByte added in v0.0.3

func HasNullByte(data []byte) bool

HasNullByteInFile checks the byte slice has the ASCII 0 or not.

Example
package main

import (
	"fmt"

	"github.com/linzeyan/utils"
)

func main() {
	type exampleType struct {
		data []byte
	}

	var x = []exampleType{
		{[]byte{0, 1, 2, 3}},
		{[]byte{44, 55, 00, 77, 88}},
		{[]byte{111, 222}},
	}
	for i := range x {
		fmt.Println(utils.HasNullByte(x[i].data))
	}
}
Output:

true
true
false

func HasNullByteInFile added in v0.0.2

func HasNullByteInFile(filePath string) (bool, error)

HasNullByteInFile checks the file has the ASCII 0, if ReadFile error returns false and error.

func HasNullByteInReader added in v0.0.2

func HasNullByteInReader(r io.Reader) (bool, error)

HasNullByteInReader checks the reader has the ASCII 0, if ReadAll error returns false and error.

func IsCIDR added in v0.0.2

func IsCIDR(i string) bool

IsCIDR checks if i is a valid CIDR.

func IsDarwin added in v0.0.2

func IsDarwin() bool

IsDarwin checks if host is darwin.

func IsDomain added in v0.0.2

func IsDomain(i any) bool

IsDomain checks if i is a valid domain.

func IsIP added in v0.0.2

func IsIP(i string) bool

IsIP checks if i is an IP address.

func IsIPv4 added in v0.0.2

func IsIPv4(i string) bool

IsIPv4 checks if i is an ipv4 address.

func IsIPv4CIDR added in v0.0.2

func IsIPv4CIDR(i string) bool

IsIPv4CIDR checks if i is a valid IPv4 CIDR.

func IsIPv6 added in v0.0.2

func IsIPv6(i string) bool

IsIPv6 checks if i is an ipv6 address.

func IsIPv6CIDR added in v0.0.2

func IsIPv6CIDR(i string) bool

IsIPv6CIDR checks if i is a valid IPv6 CIDR.

func IsLinux added in v0.0.3

func IsLinux() bool

IsLinux checks if host is linux.

func IsPathExist added in v0.0.2

func IsPathExist(f string) bool

IsPathExist checks if f is a valid path.

func IsURL added in v0.0.2

func IsURL(u string) bool

IsURL checks if u is a valid url.

func IsWindows added in v0.0.2

func IsWindows() bool

IsDarwin checks if host is windows.

func JSONMarshal added in v0.0.3

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

JSONMarshal returns the JSON encoding bytes of v.

func JSONMarshalString added in v0.0.3

func JSONMarshalString(v any) (string, error)

MarshalString returns the JSON encoding string of v.

func JSONUnmarshal added in v0.0.3

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

JSONUnmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.

func JSONUnmarshalString added in v0.0.3

func JSONUnmarshalString(data string, v any) error

JSONUnmarshalString is like JSONUnmarshal, except buf is a string.

func ListFiles

func ListFiles(dir, contentType string, fileExteinsion ...string) []string

ListFiles returns a slice containing file paths for a given content type and file extension.

func RemoveNullByte

func RemoveNullByte(data []byte) []byte

RemoveNullByte removes the ASCII 0 in the data.

Example
package main

import (
	"fmt"

	"github.com/linzeyan/utils"
)

func main() {
	type exampleType struct {
		data []byte
	}

	var x = []exampleType{
		{[]byte{0, 1, 2, 3}},
		{[]byte{44, 55, 00, 77, 88}},
		{[]byte{111, 222}},
	}
	for i := range x {
		x[i].data = utils.RemoveNullByte(x[i].data)
		fmt.Println(utils.HasNullByte(x[i].data), x[i].data)
	}
}
Output:

false [1 2 3]
false [44 55 77 88]
false [111 222]

func RemoveNullByteInFile added in v0.0.2

func RemoveNullByteInFile(filePath string) error

RemoveNullByteInFile removes the ASCII 0 in the file.

func RemoveNullByteInReader added in v0.0.2

func RemoveNullByteInReader(reader io.Reader) (io.Reader, error)

RemoveNullByteInReader removes the ASCII 0 in reader.

func ReplaceDelimiter

func ReplaceDelimiter(filePath string, old, new string) error

ReplaceDelimiter replaces the old delimiter with the new delimiter in the filePath.

func ReplaceDosToUnix

func ReplaceDosToUnix(filePath string) error

ReplaceDosToUnix replaces the Windows end of line(eol) with the Unix eol in the filePath.

func SkipFirstRow

func SkipFirstRow(f *os.File) error

SkipFirstRow skips the first row in f.

func ToInt64 added in v0.0.2

func ToInt64(v any) (int64, error)

ToInt64 converts v to int64 value, if input is not numerical, return 0 and error.

func UnZip

func UnZip(zip, dst string) error

UnZip unzips the file and save it to dst directory.

func Version added in v0.0.2

func Version() string

Version returns version information string containing git tag, git commit hash, build time, runtime and pkg path.

func Zip

func Zip(src, zip string) error

Zip creates a zip file, src could be a single file or a directory.

Types

type GoogleMaps added in v0.0.3

type GoogleMaps struct {
	Client *maps.Client
	// contains filtered or unexported fields
}

func GoogleMapsNewClient added in v0.0.3

func GoogleMapsNewClient(apiKey string) *GoogleMaps

func (*GoogleMaps) Directions added in v0.0.3

func (g *GoogleMaps) Directions(req *maps.DirectionsRequest) ([]maps.Route, []maps.GeocodedWaypoint, error)

func (*GoogleMaps) DistanceMatrix added in v0.0.3

func (*GoogleMaps) Elevation added in v0.0.3

func (g *GoogleMaps) Elevation(req *maps.ElevationRequest) ([]maps.ElevationResult, error)

func (*GoogleMaps) Geocode added in v0.0.3

func (g *GoogleMaps) Geocode(req *maps.GeocodingRequest) ([]maps.GeocodingResult, error)

func (*GoogleMaps) Geolocate added in v0.0.3

func (*GoogleMaps) PlaceDetails added in v0.0.3

func (*GoogleMaps) PlaceNearbySearch added in v0.0.3

func (g *GoogleMaps) PlaceNearbySearch(req *maps.NearbySearchRequest) (maps.PlacesSearchResponse, error)

func (*GoogleMaps) RoadsNearest added in v0.0.3

func (*GoogleMaps) RoadsSnapTo added in v0.0.3

func (g *GoogleMaps) RoadsSnapTo(req *maps.SnapToRoadRequest) (*maps.SnapToRoadResponse, error)

func (*GoogleMaps) RoadsSpeedLimits added in v0.0.3

func (g *GoogleMaps) RoadsSpeedLimits(req *maps.SpeedLimitsRequest) (*maps.SpeedLimitsResponse, error)

Jump to

Keyboard shortcuts

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