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 / example-structure.sh
Created December 28, 2020 20:14
Efs2 Project Structure Example
$ tree -L 2
.
├── CONTRIBUTING.md
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── app
│ ├── app.go
│ └── app_test.go
@madflojo
madflojo / speak.go
Last active February 13, 2021 19:41
Interfaces Article - Speak Interface
type Speak interface {
SayHello() string
}
@madflojo
madflojo / holaworld.go
Last active February 13, 2021 19:56
Interface Article - Speaking Program
package main
import (
"fmt"
)
type Speak interface {
SayHello() string
}
@madflojo
madflojo / english.go
Created February 13, 2021 19:46
Interface Article - English Implementation
type English struct{}
func (e English) SayHello() string {
return "Hello"
}
@madflojo
madflojo / speaking-english.go
Created February 13, 2021 19:49
Interface Article - Speaking English
package main
import (
"fmt"
)
type Speak interface {
SayHello() string
}
@madflojo
madflojo / powerlevel.go
Created February 14, 2021 18:29
Interface Article - PowerLevel.go
package main
import (
"fmt"
)
type Database interface {
Fetch(key string) (int, error)
}
@madflojo
madflojo / mockdb.go
Created February 14, 2021 18:41
Interface Article - MockDB Implementation
type MockDB struct {
// FakeFetch is used to provide unique test case results
FakeFetch func(string) (int, error)
}
func (m *MockDB) Fetch(k string) (int, error) {
if m.FakeFetch != nil {
return m.FakeFetch(k)
}
return 0, nil
@madflojo
madflojo / mockdb-tests.go
Created February 14, 2021 19:02
Interface Article - MockDB Tests
func TestIsOver9k(t *testing.T) {
t.Run("TestOver9000", func(t *testing.T) {
DB = &MockDB{
FakeFetch: func(string) (int, error) {
return 9001, nil
},
}
if !isOver9000() {
t.Errorf("Test did not return expected results")
}
@madflojo
madflojo / before_structure.sh
Last active May 16, 2021 21:49
Viper Article - Before Structure
$ tree -L 2
.
├── CONTRIBUTING.md
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── app
│ ├── app.go
│ ├── app_test.go
@madflojo
madflojo / app-before.go
Last active May 16, 2021 21:49
Viper Article - App Run and Stop
package app
import (
// imports go here
"github.com/madflojo/go-quick/config"
)
// Common errors returned by this app.
var (
ErrShutdown = fmt.Errorf("application shutdown gracefully")