Skip to content

Instantly share code, notes, and snippets.

View DavadDi's full-sized avatar

DavadDi DavadDi

View GitHub Profile
package main
import (
"encoding/json"
"fmt"
"reflect"
)
type SearchResult struct {
Date string `json:"date"`
import (
"bytes"
"io"
"os"
)
// not thread safe
func captureStdout(f func()) string {
old := os.Stdout
r, w, _ := os.Pipe()
@DavadDi
DavadDi / GitHub-Forking.md
Created June 16, 2017 01:29 — forked from Chaser324/GitHub-Forking.md
GitHub Standard Fork & Pull Request Workflow

Whether you're trying to give back to the open source community or collaborating on your own projects, knowing how to properly fork and generate pull requests is essential. Unfortunately, it's quite easy to make mistakes or not know what you should do when you're initially learning the process. I know that I certainly had considerable initial trouble with it, and I found a lot of the information on GitHub and around the internet to be rather piecemeal and incomplete - part of the process described here, another there, common hangups in a different place, and so on.

In an attempt to coallate this information for myself and others, this short tutorial is what I've found to be fairly standard procedure for creating a fork, doing your work, issuing a pull request, and merging that pull request back into the original project.

Creating a Fork

Just head over to the GitHub page and click the "Fork" button. It's just that simple. Once you've done that, you can use your favorite git client to clone your repo or j

@DavadDi
DavadDi / redirectExample.go
Created June 9, 2017 07:41 — forked from d-schmidt/redirectExample.go
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 {
package main
import (
"crypto/tls"
"net/http"
"time"
"log"
)
var (
codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/~!@#$%^&*()_="
codeLen = len(codes)
)
func RandNewStr(len int) string {
data := make([]byte, len)
rand.Seed(time.Now().UnixNano())
for i := 0; i < len; i++ {
#!/bin/bash
#
# Generates client and server certificates used to enable HTTPS
# remote authentication to a Docker daemon.
#
# See http://docs.docker.com/articles/https/
#
# To start the Docker Daemon:
#
# sudo docker -d \
@DavadDi
DavadDi / main.go
Created March 29, 2017 08:15
get_ip
package main
import (
"errors"
"fmt"
"net"
)
func main() {
@DavadDi
DavadDi / IsEmpty.go
Created March 22, 2017 09:15
用于检查一个变量是否为空,支持指针类型
func IsEmpty(v interface{}) bool {
rt := reflect.TypeOf(v)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
return reflect.DeepEqual(v, reflect.New(rt).Interface())
}
return reflect.DeepEqual(v, reflect.Zero(rt).Interface())
}
package main
import (
"time"
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)