Skip to content

Instantly share code, notes, and snippets.

View gist:397a73e64b8aeb56e9c484e94b8c1d82

Golang: Interface vs Struct

Some one asked me the question, how do you decide when to make an interface vs just instantiating a struct?

Honestly, this is not a perfect answer, but here are some simple guidelines that I would follow when creating an interface or just creating a simple struct.

Let's start with a simple object Person. There is nothing astronomical about the object and it contains some simple information. Here there isn't much to create

type Person struct {
@JamWils
JamWils / index.js
Created Jan 25, 2018
Stack Overflow Help
View index.js
// For help with question at https://stackoverflow.com/questions/48427217/nodejs-reading-file-does-not-work
const fs = require('fs');
const path = require('path');
console.log('beforereadfile');
fs.readFile(path.join('.', '/test.json'), 'utf8', function (err, data) {
if (err) {
console.log('Error loading client secret file: ' + err);
View DockerCommands.sh
# Delete all exited containers
docker rm $(docker ps -q -f status=exited)
# Delete all images with no tags
docker rmi $(docker images -q -f "dangling=true")
# Plugins -> Network
docker info
# Create a new network
@JamWils
JamWils / Git Aliases
Last active Jun 15, 2020
Git aliases I use on a day-to-day basis
View Git Aliases
git config --global alias.s "status -s"
git config --global alias.bc="branch --sort=-committerdate"
git config --global alias.lg "log --oneline --decorate --graph"
git config --global alias.lgb "log --graph --oneline --decorate --first-parent"
// Calling log for branch
git lgb <branch_name>
View firebaseDatabaseSet.js
const client = require('firebase-tools');
client.database.set('/', './mockData/db.json', {
project: 'my-test-project',
token: process.env.FIREBASE_TOKEN,
cwd: './'
});
View generatingJSON.js
const json = mockDataSchema.schema();
const jsonFormatted = JSON.stringify(json);
fs.writeFile("./mockData/db.json", jsonFormatted, (err) => {
if (err) {
return console.log(chalk.red(err));
} else {
console.log(chalk.green("Mock data generated."));
}
});
View exportChance.js
exports.schema = function () {
let parent = {}
parent.users = createUsers(3, 7);
return parent;
}
View randomChance.js
function randomIntInc(low, high) {
return Math.floor(Math.random() * (high - low + 1) + low);
}
function createUsers(low, high) {
let items = {};
const generatedItems = randomIntInc(low, high);
for (let i = 0; i < generatedItems; i++) {
const item = {}
@JamWils
JamWils / chanceSample.js
Last active Jun 8, 2017
Small Chance Sample
View chanceSample.js
const item = {}
item.uid = chance.guid();
item.email = chance.email();
item.profileImageUrl = chance.avatar();
item.username = chance.name();
View gist:3d88d82ad66371139c9765f24200f5e0
def h_levelsum(self) -> int:
'''The sum of the level costs of the individual goals (admissible if goals independent)
:return: int
'''
level_sum = 0
# TODO implement
# for each goal in the problem, determine the level cost, then add them together
for goal in self.problem.goal:
levels = [level for level in range(len(self.s_levels)) for state in self.s_levels[level] if goal == state.literal]