This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func doSomething(v interface{}) { | |
// v argument has type interface{} not any type | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
// define interfaces | |
type Vehicle interface { | |
Move() string | |
} | |
// define structs and implement Vehicle interface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [1, 2, 3, 4, 5] | |
print(a[0], a[-1]) # 1 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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') ... | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
OlderNewer