goessentials

package module
v0.0.0-...-765e882 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: GPL-3.0 Imports: 4 Imported by: 1

README

GoEssentials

This repository provides functions that are generally useful for many types of projects.

It provides:

  • Many useful functions
  • Examples showing how to use each function

Table of Contents:

About

GoEssentials is a grouping of all the functions I've found myself repeatedly copy/pasting from project to project. Many functions, like GetConfig(), are useful in a wide range of projects spanning web development, cli applications, usermode services, and more!

Installing and Compiling from Source

In order to use functions from GoEssentials, you'll need the following:

Then simply get GoEssentials and start using it!

go get github.com/ChristianHering/GoEssentials

Contributing

Contributions are always welcome. If you're interested in contributing, send me an email or submit a PR.

License

This project is currently licensed under GPLv3. This means you may use our source for your own project, so long as it remains open source and is licensed under GPLv3.

Please refer to the license file for more information.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrorReadConfigFailed      = errors.New("Unable to read config")
	ErrorUnmarshalConfigFailed = errors.New("Unable to unmarshal config")
	ErrorMarshalConfigFailed   = errors.New("Unable to marshal config")
	ErrorWriteConfigFailed     = errors.New("Unable to write config")
	ErrorConfigFileUnset       = errors.New("Config file wasn't set prior to call")
)

Functions

func FileExists

func FileExists(filename string) bool

FileExists checks to see if a file exists and returns true if it exists

Example
if FileExists("filesystemExist_test.go") {
	log.Println("Just making sure I'm still here")
} else {
	log.Fatalln("We seem to have disappeared")
}
Output:

func FileNotExist

func FileNotExist(filename string) bool

FileNotExist checks to see if a file exists and returns false if it exists

Example
if FileNotExist("DoesNotExist.here") {
	log.Println("The file doesn't exist... Cool!")
} else {
	log.Fatalln("This file shouldn't exist")
}
Output:

func FolderExists

func FolderExists(path string) bool

FolderExists checks to see if a dir exists and returns true if it exists

Example
if FolderExists("../GoEssentials") {
	log.Println("Just checking in on my parent")
} else {
	log.Fatalln("Where'd Go go this time")
}
Output:

func FolderNotExist

func FolderNotExist(path string) bool

FolderNotExist checks to see if a dir exists and returns false if it exists

Example
if FolderNotExist("DoesNotExist") {
	log.Println("It'd be weird if that directory was a thing anyway")
} else {
	log.Fatalln(`Did you just create a directory called "DoesNotExist"?`)
}
Output:

func GetConfig

func GetConfig(configFileName string, configPointer interface{}) error

GetConfig fetches the config located at 'configFileName' and puts its parsed contents into 'configPointer' if the config doesn't exist, it's created with the default from 'configPointer' and returns the error 'ErrorConfigFileUnset'

Example

ExampleGetConfig provides a simple example

package main

import (
	"log"
)

type ExampleConfig struct {
	Works bool
}

// This both serves as the default configuration
// and a global config variable. If you don't
// want your config to be globally accessable,
// you could also define this somewhere else.
var config = ExampleConfig{
	Works: true,
}

// ExampleGetConfig provides a simple example
func main() {
	err := GetConfig("exampleConfig.json", &config)
	if err != nil && err != ErrorConfigFileUnset {
		log.Fatalln(err)
	}
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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