Skip to content

Instantly share code, notes, and snippets.

@techslides
techslides / template-eq-check.go
Last active November 2, 2022 19:26
Equality checking in Go Lang Templates
{{if eq .authuser.Id .user.Id }}<button type="button" class="delete_user" rel="{{.user.Id}}">Delete My Account</button>{{end}}
@techslides
techslides / golang-prettyurl.go
Created February 13, 2014 16:38
Generating Pretty Urls for SEO in GoLang using string and regex packages
type Post struct {
// db tag lets you specify the column name if it differs from the struct field
Id int64 `db:"post_id"`
Created int64
Title string `form:"Title" binding:"required"`
Body string `form:"Body"`
UserId int64 `form:"UserId"`
Url string
}
// jXHR.js (JSON-P XHR) | v0.1 (c) Kyle Simpson | MIT License
(function(c){var b=c.setTimeout,d=c.document,a=0;c.jXHR=function(){var e,g,n,h,m=null;function l(){try{h.parentNode.removeChild(h)}catch(o){}}function k(){g=false;e="";l();h=null;i(0)}function f(p){try{m.onerror.call(m,p,e)}catch(o){throw new Error(p)}}function j(){if((this.readyState&&this.readyState!=="complete"&&this.readyState!=="loaded")||g){return}this.onload=this.onreadystatechange=null;g=true;if(m.readyState!==4){f("Script failed to load ["+e+"].")}l()}function i(o,p){p=p||[];m.readyState=o;if(typeof m.onreadystatechange==="function"){m.onreadystatechange.apply(m,p)}}m={onerror:null,onreadystatechange:null,readyState:0,open:function(p,o){k();internal_callback="cb"+(a++);(function(q){c.jXHR[q]=function(){try{i.call(m,4,arguments)}catch(r){m.readyState=-1;f("Script failed to run ["+e+"].")}c.jXHR[q]=null}})(internal_callback);e=o.replace(/=\?/,"=jXHR."+internal_callback);i(1)},send:function(){b(function(){h=d.createElement("script");h.setAttr
@techslides
techslides / api-go-martini.go
Created August 28, 2014 16:43
Static Response for API endpoint with Go, Martini, and GAE
package hello
import (
"net/http"
"github.com/go-martini/martini"
"github.com/martini-contrib/cors"
"github.com/martini-contrib/render"
)
type Resources struct {
@techslides
techslides / cards-shuffle.go
Last active January 4, 2023 05:34
Shuffle Array of Objects with Go Lang
package hello
import (
"net/http"
"github.com/go-martini/martini"
"github.com/martini-contrib/cors"
"github.com/martini-contrib/render"
"math/rand"
)
@techslides
techslides / golang-split-shuffle.go
Last active August 29, 2015 14:05
Go Lang Route to Split and Shuffle Array of Objects
m.Get("/cards/:players", allowCORSHandler, func(args martini.Params, r render.Render) {
Shuffle(cards)
//use Atoi as ParseInt always does int64
p, _ := strconv.Atoi(args["players"])
if(p==0 || p>52){
//not valid number
r.JSON(200, cards)
@techslides
techslides / github-gist-api.js
Last active January 1, 2022 23:03
GitHub API to make Gists with Ajax
/*
Assuming jQuery Ajax instead of vanilla XHR
*/
//Get Github Authorization Token with proper scope, print to console
$.ajax({
url: 'https://api.github.com/authorizations',
type: 'POST',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa("USERNAME:PASSWORD"));
@techslides
techslides / basic.html
Created October 2, 2014 16:28
Basic HTML5 Starter Template
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Page Description for Search Engines"/>
<title>HTML5 Basic Structure</title>
<link rel="stylesheet" href="http://necolas.github.io/normalize.css/3.0.1/normalize.css">
@techslides
techslides / form.html
Last active July 8, 2018 22:51
Form with Client-Side JS
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form Calculator Add Example</title>
</head>
<body>
<form name="myForm" action="" onsubmit="return calc(this['A'].value,this['B'].value);" method="post">
<input type="text" name="A"> +
<input type="text" name="B"> =
@techslides
techslides / form1.js
Last active August 29, 2015 14:07
Simple Node HTTP server
// Load the http module to create an http server.
var http = require('http');
// Create a function to handle every HTTP request
function handler(req, res){
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.end("<html><body><h1>Hello</h1></body></html>");
};