seq

package module
v0.0.0-...-88f7f77 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2019 License: BSD-3-Clause Imports: 15 Imported by: 0

README

Seq

Godoc Reference

Package for encoding/decoding data sequences.

Usage

$ go get github.com/epiqm/seq

Example

Marshal object to JSON text and unmarshal from JSON text string to custom object.

// main.go
package main

import (
        "fmt"

        "github.com/epiqm/seq"
)

func main() {
        // Get MD5 hash from text string
        hash := seq.Hash("text string")

        fmt.Println(hash)
        // a278c7ab35780d23f34c75dd23278b4b
}
Other examples
Get MD5 hash from text string
hash := seq.Hash("text string")

fmt.Println(hash)
// a278c7ab35780d23f34c75dd23278b4b
Marshal object to JSON text
obj := map[string]interface{}{
        "username": "Max",
        "age":      27,
        "hometown": "Kiev",
}
json := seq.Marshal(obj)

fmt.Println(json)
// Output:
// {"age":27,"hometown":"Kiev","username":"Max"}
Unmarshal JSON text to Go object
// Author example object for unmarshal
type Author struct {
        Username string `json:"username"`
        Hometown string `json:"hometown"`
        Age      int    `json:"age"`
}

var newObj Author
seq.Unmarshal(json, &newObj)

fmt.Println(newObj.Username)
// Output:
// Max
Base64 encode
key := ""
text := "A message for encoding."

s, err := seq.Encode(text, key)
if err != nil {
        fmt.Println(err)
        return
}

fmt.Println(s)
// Output:
// QSBtZXNzYWdlIGZvciBlbmNvZGluZy4=
Base64 decode
key := ""
text := "QSBtZXNzYWdlIGZvciBlbmNvZGluZy4="

s, err := seq.Decode(text, key)
if err != nil {
        fmt.Println(err)
        return
}

fmt.Println(s)
// Output:
// A message for encoding.
Get file size in bytes
fsize, err := seq.GetFileSize("testing.txt")
if err != nil {
        fmt.Println(err)
        return
}
Create a file
err := seq.CreateFile("testing.txt")
if err != nil {
        fmt.Println(err)
        return
}
Write a file
err := seq.WriteFile("testing.txt", "A testing text file.")
if err != nil {
        fmt.Println(err)
        return
}
Read a file
text, err := seq.ReadFile("testing.txt")
if err != nil {
        fmt.Println(err)
        return
}

fmt.Println(text)
// Output:
// A testing text file.
Copy a file
err := seq.CopyFile("testing.txt", "testing.txt.copy")
if err != nil {
        fmt.Println(err)
        return
}
Move file
err := seq.MoveFile("testing.txt.copy", "testing.txt")
if err != nil {
        fmt.Println(err)
        return
}
Base64 encode file contents and resave
err := seq.EncodeFile("testing.txt", "")
if err != nil {
        fmt.Println(err)
        return
}
Base64 decode contents of a file and resave
err := seq.DecodeFile("testing.txt", "")
if err != nil {
        fmt.Println(err)
        return
}
Delete file
err := seq.RmFile("testing.txt")
if err != nil {
        fmt.Println(err)
        return
}
Create a directory
err := seq.CreateDir("newdir")
if err != nil {
        fmt.Println(err)
        return
}
List directory files
files, err := seq.Ls("newdir")
if err != nil {
        fmt.Println(err)
        return
}
Remove directory (recursively, removes subfolders/files)
err := seq.RmDir("newdir")
if err != nil {
        fmt.Println(err)
        return
}

Credits

Copyright (c) 2018 Maxim R. All rights reserved.

For feedback or questions <epiqmax@gmail.com>

Documentation

Overview

Package for encoding/decoding data sequences.

A functional library for performing basic data operations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyFile

func CopyFile(path string, newpath string) error

Copies file to the new path.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	err := seq.CopyFile("testing.txt", "testing.txt.copy")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("OK")
}
Output:

OK

func CreateDir

func CreateDir(path string) error

Creates a directory.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	err := seq.CreateDir("newdir")
	if err != nil {
		fmt.Println(err)
		return
	}

	err = seq.WriteFile("newdir/testing.txt", "Another test file.")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("OK")
}
Output:

OK

func CreateFile

func CreateFile(path string) error

Creates a file.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	err := seq.CreateFile("testing.txt")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("OK")
}
Output:

OK

func Decode

func Decode(text string, key string) (s string, err error)

Decodes/decrypts Base64 string.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	key := ""
	text := "QSBtZXNzYWdlIGZvciBlbmNvZGluZy4="

	s, err := seq.Decode(text, key)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(s)
}
Output:

A message for encoding.

func DecodeFile

func DecodeFile(path string, key string) error

Decodes/decrypts contents and rewrites the file.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	err := seq.DecodeFile("testing.txt", "")
	if err != nil {
		fmt.Println(err)
		return
	}

	text, err := seq.ReadFile("testing.txt")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(text)
}
Output:

A testing text file.

func Encode

func Encode(text string, key string) (s string, err error)

Returns Base64 encoded string from text. Uses AES(CBC) encryption if key is present.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	key := ""
	text := "A message for encoding."

	s, err := seq.Encode(text, key)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(s)
}
Output:

QSBtZXNzYWdlIGZvciBlbmNvZGluZy4=

func EncodeFile

func EncodeFile(path string, key string) error

Encodes/encrypts contents and rewrites the file.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	err := seq.EncodeFile("testing.txt", "")
	if err != nil {
		fmt.Println(err)
		return
	}

	text, err := seq.ReadFile("testing.txt")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(text)
}
Output:

QSB0ZXN0aW5nIHRleHQgZmlsZS4=

func GetFileSize

func GetFileSize(path string) (int64, error)

Gets length of a file (in bytes) Returns size as int64

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	err := seq.WriteFile("testing.txt", "MAX")
	if err != nil {
		fmt.Println(err)
		return
	}

	fsize, err := seq.GetFileSize("testing.txt")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(fsize)
}
Output:

3

func Hash

func Hash(text string) string

Performs MD5 hashing operation on given text.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	hash := seq.Hash("some text")

	fmt.Println(hash)
}
Output:

552e21cd4cd9918678e3c1a0df491bc3

func HashCut

func HashCut(text string, length int) string

Performs MD5 hashing operation on given text. Returns string with specified length.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	shortHash := seq.HashCut("some text", 3)

	fmt.Println(shortHash)
}
Output:

552

func Ls

func Ls(path string) (fm []string, err error)

Lists a directory.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	files, err := seq.Ls("newdir")
	if err != nil {
		fmt.Println(err)
		return
	}

	for _, f := range files {
		fmt.Println(f)
	}

}
Output:

testing.txt

func Marshal

func Marshal(m interface{}) (s string)

Outputs JSON string from structure.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	// prepare our object to perform marshal
	obj := map[string]interface{}{
		"username": "Max",
		"age":      27,
		"hometown": "Kiev",
	}

	// marshal object to string
	str := seq.Marshal(obj)

	fmt.Println(str)
}
Output:

{"age":27,"hometown":"Kiev","username":"Max"}

func MoveFile

func MoveFile(oldpath string, newpath string) error

Moves file to the new path.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	err := seq.MoveFile("testing.txt.copy", "testing.txt")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("OK")
}
Output:

OK

func Percent

func Percent(s, f int) (delta float64)

Gets percentage of two values. Note: first value should be lower.

func Percentage

func Percentage(s, f int) string

Returns percentage of two values.

func Rand

func Rand(min, max int) int

Generates random number between min and max.

func ReadFile

func ReadFile(path string) (string, error)

Reads file contents to string.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	text, err := seq.ReadFile("testing.txt")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(text)
}
Output:

A testing text file.

func RmDir

func RmDir(path string) error

Removes a directory.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	err := seq.RmDir("newdir")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("OK")
}
Output:

OK

func RmFile

func RmFile(path string) error

Deletes file.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	err := seq.RmFile("testing.txt")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("OK")
}
Output:

OK

func RoundUp

func RoundUp(input float64, places int) (rounded float64)

Returns rounded float64 number.

func Unmarshal

func Unmarshal(text string, s interface{})

Unmarshals JSON string to structure.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	// prepare author structure
	type Author struct {
		Username string `json:"username"`
		Hometown string `json:"hometown"`
		Age      int    `json:"age"`
	}

	// prepare json text string
	json := `{"age":27,"hometown":"Kiev","username":"Max"}`

	// create author variable
	var obj Author

	// unmarshal json text string to object
	seq.Unmarshal(json, &obj)

	fmt.Println(obj)
}
Output:

{Max Kiev 27}

func WriteFile

func WriteFile(path string, text string) error

Writes text string to file.

Example
package main

import (
	"fmt"

	"seq"
)

func main() {
	err := seq.WriteFile("testing.txt", "A testing text file.")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("OK")
}
Output:

OK

Types

This section is empty.

Jump to

Keyboard shortcuts

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