Skip to content

Instantly share code, notes, and snippets.

@rickt
rickt / bzip-inline-decompress.go
Last active June 16, 2021 10:14
HOW-TO: inline-decompress a .bz2 file with Go
package main
import (
"bufio"
"compress/bzip2"
"fmt"
"io"
"os"
)
@rickt
rickt / smtp-send.go
Last active December 30, 2015 10:39
HOW-TO: send email via SMTP with Go.
package main
import (
"crypto/tls"
"fmt"
"log"
"net"
"net/mail"
"net/smtp"
)
@rickt
rickt / blowfish.go
Last active December 30, 2015 10:59
example code to blowfish encrypt/decrypt, using Go.
package main
import (
"crypto/cipher"
"fmt"
"go.crypto/blowfish"
)
func blowfishChecksizeAndPad(pt []byte) []byte {
// calculate modulus of plaintext to blowfish's cipher block size
@rickt
rickt / isascii.go
Last active August 29, 2015 14:02
check to see if something is ASCII or not
package main
import "fmt"
func isASCII(s string) bool {
for _, c := range s {
if c > 127 {
return false
}
}
package main
import "fmt"
import "regexp"
var email = regexp.MustCompile(`^[^@]+@[^@.]+\.[^@.]+$`)
var shortPhone = regexp.MustCompile(`^[0-9][0-9][0-9][.\-]?[0-9][0-9][0-9][0-9]$`)
var longPhone = regexp.MustCompile(`^[(]?[0-9][0-9][0-9][). \-]*[0-9][0-9][0-9][.\-]?[0-9][0-9][0-9][0-9]$`)
func main() {
@rickt
rickt / analytics-dumper.go
Last active April 8, 2016 02:48
example Go code showing how to download reporting data from Google Analytics using the Core Reporting API (updated 2015)
//
// EDIT: this code is now old
// i have recently (april 2016) updated it to use the new golang.org/x/oauth2 lib
// you can get the new analytics dumper at the below url:
// https://gist.github.com/rickt/d839564155cac15d59b6027668d8cb64
//
package main
import (
@rickt
rickt / url-escaping.go
Created June 20, 2014 21:20
how to properly escape URLs in go
package main
import (
"fmt"
"net/url"
)
func main() {
var Url *url.URL
@rickt
rickt / randomstring.go
Created June 20, 2014 21:23
generate a random string
package main
import (
"crypto/rand"
"fmt"
)
func main() {
fmt.Printf("randstring = %s\n", rand_str(5)) // change "5" to be whatever length string you need
@rickt
rickt / check-if-jpeg.go
Last active August 29, 2015 14:02
check if a file really is a JPEG or not
package main
import (
"fmt"
"image"
"image/jpeg"
"os"
)
func checkIsJPEG(filename string) error {
@rickt
rickt / asynchttpget.go
Last active August 29, 2015 14:02
example of how to do asynchronous http gets
package main
import (
"fmt"
"net/http"
"time"
)
var urls = []string{
"http://rickt.org",