Skip to content

Instantly share code, notes, and snippets.

View ericelsken's full-sized avatar
🏠
Working from home

Eric Elsken ericelsken

🏠
Working from home
View GitHub Profile
@ericelsken
ericelsken / user_options.go
Last active October 7, 2019 23:54
User Function Options
package user
import (
"bytes"
"errors"
"fmt"
"time"
"github.com/google/uuid"
)
@ericelsken
ericelsken / function_option_tldr.go
Created October 6, 2019 19:58
Function Option TL;DR
package entity
type Entity struct {
fieldOne string
}
type Option func(e *Entity)
func FieldOne(fieldOne string) Option {
return func(e *Entity) {
//Build uses config and its factory functions to build a new App.
//If err is nil, the returned App is ready to Run.
func (ab *AppBuilder) Build(config *config.Config) (app *App, err error) {
var sqlRepo *sqlrepo.Repo
var dbCloser io.Closer
defer func() {
if err != nil && dbCloser != nil {
dbCloser.Close()
}
@ericelsken
ericelsken / clients_api_handler.go
Created July 18, 2018 15:48
Example http.Handlers for client endpoints. From https://github.com/AgencyPMG/go-from-scratch
//getClient retrieves and sends a single Client.
func (a *API) getClient(w http.ResponseWriter, r *http.Request) {
client, ok := a.getClientEntity(w, r)
if !ok {
return
}
a.sendData(w, client, http.StatusOK)
}
//Handler is a handler type that understands how to work with Client commands.
type Handler struct {
//Clients is the Client repository to use within Command handling.
Clients client.Repo
}
//CreateClient attempts to create a new Client and add it to h.Clients.
//Cmd must be of type *CreateClientCommand.
//The result, if not nil and without error, will be a *client.Client.
func (h *Handler) CreateClient(ctx context.Context, cmd cbus.Command) (interface{}, error) {
@ericelsken
ericelsken / user_repo.go
Last active July 17, 2018 23:03
User QueryRepo and Repo types. From https://github.com/AgencyPMG/go-from-scratch
//QueryRepo provides methods for retrieving Users.
type QueryRepo interface {
//Get should return the User whose Id equals id.
//
//An error should be returned if the User does not exist or there was an error
//attempting to load the User.
Get(ctx context.Context, id data.Id) (*User, error)
//GetEmail should return the User whose Email equals email.
//
import React, { Component, PropTypes } from 'react';
export default class ClientDashboard extends Component {
static propTypes = {
clientId: PropTypes.string.isRequired,
client: PropTypes.object,
fetchClient: PropTypes.func.isRequired,
}
componentWillMount() {
@ericelsken
ericelsken / main.go
Created May 13, 2015 20:08
Print number repeated number of times.
package main
import (
"fmt"
)
func main() {
for i := 1; i <= 9; i++ {
for count := 0; count < i; count++ {
fmt.Print(i)