Skip to content

Instantly share code, notes, and snippets.

@bolerap
bolerap / serve.go
Created September 27, 2016 07:22 — forked from paulmach/serve.go
Simple Static File Server in Go
/*
Serve is a very simple static file server in go
Usage:
-p="8100": port to serve on
-d=".": the directory of static files to host
Navigating to http://localhost:8100 will display the index.html or directory
listing file.
*/
package main
@bolerap
bolerap / golang-interface.go
Last active November 2, 2016 05:55
Golang interface
func doSomething(v interface{}) {
// v argument has type interface{} not any type
}
package main
import "fmt"
// define interfaces
type Vehicle interface {
Move() string
}
// define structs and implement Vehicle interface
# 1
a, b, c = 1, 2, 3
d = a, b, c
print(d, type(d)) # (1, 2, 3) tuple
# 2
a, b, c = (2 * i + 1 for i in range(3))
d = a, b, c
print(d) # (1, 3, 5)
a = [1, 2, 3, 4, 5]
print(a[0], a[-1]) # 1 5
a = [1, 2, 3, 4, 5]
# slice [start:end]
print(a[1:3]) # [2 3]
print(a[-3: -1]) # [3, 4]
# slice [start:end:step]
print(a[::2]) # [1 3 5]
print(a[::-1]) # [5 4 3 2 1]
# list
a = ['Hello', "World", 'Python']
for i, v in enumerate(a):
print(i, v)
# Dictionary
a = {'name': 'Peter', 'age': 25}
for k, v in a.items(): # .iteritems() with python 2
print(k, v)
# zipping: python 3 return a zip object, python 2 return a list of tuples
a = [1, 2, 3]
b = ['a', 'b', 'c']
zipped = zip(a, b) # This result will only live for once used then zipped will become empty
# to print value of zip
# 1 use for loop as a iterator
for x in zipped:
print(x) # (1, 'a') ...
# Send GET request to github api to retrieve info of an github user without header
curl https://api.github.com/users/<username>
# Send GET request to github api to retrieve info of an github user with header
curl --include https://api.github.com/users/<username>
# or
curl -i https://api.github.com/users/<username>
# Send GET request with basic auth use user credential to github api to retrieve protected resources
curl --user "<username>:<password>" https://api.github.com/users/<username>
# Send POST request with data to github api to create a gist
curl --user "<username>" --request POST --data '{"description": "Test create a github gists via api", "public": "true", "files": {"test.txt": {"content": "Hello"}}}' https://api.github.com/gists
# or in short
curl -u "<username>" -X POST -d '{"description": "Test create a github gists via api", "public": "true", "files": {"test.txt": {"content": "Hello"}}}' https://api.github.com/gists
# or more short, if supplied -d (data) POST method can ommited as below
curl -u "<username>" -d '{"description": "Test create a github gists via api", "public": "true", "files": {"test.txt": {"content": "Hello"}}}' https://api.github.com/gists
# Send POST request with multiple data
curl --data "login=<username>" --data "token=<token_string>" https://github.com/api/v2/json/user/show/<username>
# or combine into single --data