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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / new-func.go
Created August 21, 2022 23:50
Threadsafe Packages Article: New
// New is used to create a new instance of Directory.
func New(name string) (*Directory, error) {
return &Directory{}, nil
}
@madflojo
madflojo / logic.go
Created August 24, 2022 13:38
Threadsafe Packages - Base with Logic but unsafe
/*
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
import (