Skip to content

Instantly share code, notes, and snippets.

View astockwell's full-sized avatar

Alex Stockwell astockwell

View GitHub Profile
@mholt
mholt / macapp.go
Last active April 8, 2024 17:54
Distribute your Go program (or any single binary) as a native macOS application
// Package main is a sample macOS-app-bundling program to demonstrate how to
// automate the process described in this tutorial:
//
// https://medium.com/@mattholt/packaging-a-go-application-for-macos-f7084b00f6b5
//
// Bundling the .app is the first thing it does, and creating the DMG is the
// second. Making the DMG is optional, and is only done if you provide
// the template DMG file, which you have to create beforehand.
//
// Example use:
@jesselucas
jesselucas / changroup.go
Last active April 17, 2024 14:33
Simple solution for using a WaitGroup with select{}
package main
import (
"fmt"
"sync"
"time"
)
func main() {
// Create a wait group of any size
@xDarkicex
xDarkicex / gzip.go
Created November 5, 2016 05:16
Example of caching and gzip, httprouter
package gzip
import (
"compress/gzip"
"io"
"net/http"
"strings"
"github.com/julienschmidt/httprouter"
)
@d-schmidt
d-schmidt / redirectExample.go
Last active March 12, 2024 08:04
How to redirect HTTP to HTTPS with a golang webserver.
package main
import (
"net/http"
"log"
)
func redirect(w http.ResponseWriter, req *http.Request) {
// remove/add not default ports from req.Host
target := "https://" + req.Host + req.URL.Path
if len(req.URL.RawQuery) > 0 {
@astockwell
astockwell / page.php
Last active August 29, 2015 14:06
Advanced Custom Fields Pristine Field Uses
<!-- Text Field -->
<?php $field_name_slug = get_field("field_name_slug"); if ( !empty($field_name_slug) ): ?>
<?php echo $field_name_slug; ?>
<?php endif; ?>
<!-- Image Field -->
<?php $image_field_name = get_field("image_field_name"); if ( $image_field_name ): ?>
<img src="<?php echo $image_field_name["url"]; ?>" alt="<?php echo $image_field_name["title"]; ?>" />
<?php endif; ?>
@andreagrandi
andreagrandi / parse_json_post.go
Created August 19, 2014 13:28
Parse a JSON http POST in GoLang
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type test_struct struct {
Test string
@mattes
mattes / check.go
Last active May 3, 2024 22:20
Check if file or directory exists in Golang
if _, err := os.Stat("/path/to/whatever"); os.IsNotExist(err) {
// path/to/whatever does not exist
}
if _, err := os.Stat("/path/to/whatever"); !os.IsNotExist(err) {
// path/to/whatever exists
}
package main
import (
"net/http"
"database/sql"
"fmt"
"log"
"os"
)
<?php
/*
* Reference
*
* Term object keys:
* [term_id, name, slug, term_group, term_taxonomy_id, taxonomy, description, parent]
*
*/
@justincase
justincase / gist:5469009
Created April 26, 2013 17:45
Print struct with field names and values. From http://blog.golang.org/2011/09/laws-of-reflection.html
type T struct {
A int
B string
}
t := T{23, "skidoo"}
s := reflect.ValueOf(&t).Elem()
typeOfT := s.Type()
for i := 0; i < s.NumField(); i++ {