Skip to content

Instantly share code, notes, and snippets.

View madflojo's full-sized avatar
:octocat:
Writing code that might be useful

Benjamin Cane madflojo

:octocat:
Writing code that might be useful
View GitHub Profile
@madflojo
madflojo / directory_base.go
Last active August 23, 2022 02:27
Threadsafe Packages Article: Base Structure
/*
Package directory is an example package that creates an internal directory for People.
This package is an example used for a Blog article and is not for anything else.
*/
package directory
// Person represents a person and their attributes.
@madflojo
madflojo / shellcheck.out
Created July 7, 2022 15:47
Article - Managing Shell Scripts - Shellcheck output
$ shellcheck whoami.sh
In whoami.sh line 6:
if [ $WHOAMI == "root" ]; then
^-----^ SC2086 (info): Double quote to prevent globbing and word splitting.
Did you mean:
if [ "$WHOAMI" == "root" ]; then
For more information:
@madflojo
madflojo / whoami.sh
Created July 7, 2022 15:44
Article - Managing Shell Scripts - Post shfmt
#! /bin/bash
# Simple script that says who I am.
WHOAMI=$(whoami)
if [ $WHOAMI == "root" ]; then
cowsay "I am rooooot!!!"
else
cowsay "You are $WHOAMI"
fi
@madflojo
madflojo / whoami.sh
Created July 7, 2022 15:41
Article - Manage Shell Scripts - Pre formatter
#! /bin/bash
# Simple script that says who I am.
WHOAMI=`whoami`
if [ $WHOAMI == "root" ]
then
cowsay "I am rooooot!!!"
else
cowsay "You are $WHOAMI"
@madflojo
madflojo / keepalive-intvl.go
Created May 27, 2022 14:51
TCP Keepalive Intervals
// Set Keepalive time interval
err = c.SetKeepAlivePeriod(30 * time.Second)
if err != nil {
fmt.Printf("Unable to set keepalive interval - %s", err)
}
@madflojo
madflojo / tcpclient.go
Last active May 27, 2022 14:51
TCPClient - Keepalives
// Open TCP Connection
c, err := net.DialTCP("tcp", nil, addr)
if err != nil {
fmt.Printf("Unable to dial to server - %s", err)
}
// Enable Keepalives
err = c.SetKeepAlive(true)
if err != nil {
fmt.Printf("Unable to set keepalive - %s", err)
@madflojo
madflojo / tcpserver.go
Last active May 27, 2022 14:51
TCP KeepAlives Server Example
// Resolve TCP Address
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:9000")
if err != nil {
fmt.Printf("Unable to resolve IP")
}
// Start TCP Listener
l, err := net.ListenTCP("tcp", addr)
if err != nil {
fmt.Printf("Unable to start listener - %s", err)
@madflojo
madflojo / app.go
Created May 13, 2022 14:58
Go Project Structure - App Run
// cfg is used across the app package to contain configuration.
var cfg *viper.Viper
// Run starts the primary application. It handles starting background services,
// populating package globals & structures, and clean up tasks.
func Run(c *viper.Viper) error {
// Apply config provided by main to the package global
cfg = c
...
@madflojo
madflojo / app_test.go
Last active May 13, 2022 15:36
Go Project Structure - Testing App
func TestRunningServer(t *testing.T) {
cfg := viper.New()
cfg.Set("disable_logging", true)
cfg.Set("listen_addr", "localhost:9000")
cfg.Set("kvstore_type", "redis")
cfg.Set("redis_server", "redis:6379")
cfg.Set("enable_kvstore", true)
cfg.Set("debug", true)
cfg.Set("trace", true)
go func() {
@madflojo
madflojo / main.go
Created May 11, 2022 13:49
Go Project Structure - main.go
func main() {
// Initiate a simple logger
log := logrus.New()
// Setup Config
cfg := viper.New()
// Set Default Configs
cfg.SetDefault("enable_tls", true)
cfg.SetDefault("listen_addr", "0.0.0.0:8443")