Skip to content

simple go module that provides commonly used go-tools

License

Notifications You must be signed in to change notification settings

barcollin/toolkit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Golang toolkit

A simple go module that provides commonly used go-tools

Features

  • Random String Generator - Generates random string of n lenght based on these characters abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_
  • Files Upload - Upload files and generate random filenames using Random string Generator , it also support file size limits and file types customization
  • Creating Directory - Create directories it they don't exist
  • Generating Slugs - Generate slugs from a string
  • Downloading Static Files - Download files and forces browser not to display such files
  • Read JSON - provides quick way to read JSON
  • Write JSON - provides quick way to read JSON
  • Error JSON - produces a JSON encoded error repsonse
  • Post JSON - pPosts JSON to a remote service

Installation

go get -u github.com/barcollin/toolkit

Examples

Random String Generation

package main

import (
	"fmt"

	"github.com/barcollin/toolkit"
)

func main() {
	var tools toolkit.Tools

	s := tools.RandomString(20)

	fmt.Println("Random string:", s)

}

Creating a Directory if it doesn't exist

package main

import (
	"github.com/barcollin/toolkit"
)

func main() {
	var tools toolkit.Tools

	tools.CreateDirIfNotExist("./test-dir")
}

Slugifying a string

package main

import (
	"log"

	"github.com/barcollin/toolkit"
)

func main() {
	toSlug := "NOW!!! is the time 123"

	var tools toolkit.Tools

	slugified, err := tools.Slugify(toSlug)
	if err != nil {
		log.Println(err)
	}

	log.Println(slugified)

}

Downloading a static file

package main

import (
	"log"
	"net/http"

	"github.com/barcollin/toolkit"
)

func main() {
	// get routes
	mux := routes()

	// start a server
	err := http.ListenAndServe(":8080", mux)
	if err != nil {
		log.Fatal(err)
	}

}

func routes() http.Handler {
	mux := http.NewServeMux()

	mux.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("."))))
	mux.HandleFunc("/download", downloadFile)

	return mux

}

func downloadFile(w http.ResponseWriter, r *http.Request) {
	t := toolkit.Tools{}
	t.DownloadStaticFile(w, r, "./files", "img.png", "puppy.png")
}

About

simple go module that provides commonly used go-tools

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages