Documentation
¶
Overview ¶
Package for encoding/decoding data sequences.
A functional library for performing basic data operations.
Index ¶
- func CopyFile(path string, newpath string) error
- func CreateDir(path string) error
- func CreateFile(path string) error
- func Decode(text string, key string) (s string, err error)
- func DecodeFile(path string, key string) error
- func Encode(text string, key string) (s string, err error)
- func EncodeFile(path string, key string) error
- func GetFileSize(path string) (int64, error)
- func Hash(text string) string
- func HashCut(text string, length int) string
- func Ls(path string) (fm []string, err error)
- func Marshal(m interface{}) (s string)
- func MoveFile(oldpath string, newpath string) error
- func Percent(s, f int) (delta float64)
- func Percentage(s, f int) string
- func Rand(min, max int) int
- func ReadFile(path string) (string, error)
- func RmDir(path string) error
- func RmFile(path string) error
- func RoundUp(input float64, places int) (rounded float64)
- func Unmarshal(text string, s interface{})
- func WriteFile(path string, text string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CopyFile ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ReadFile ¶
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 ¶
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 ¶
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 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}
Types ¶
This section is empty.