Skip to content

Instantly share code, notes, and snippets.

View iamjjanga-ouo's full-sized avatar
🐵
i can do this all day

LeeSihyung iamjjanga-ouo

🐵
i can do this all day
View GitHub Profile
@iamjjanga-ouo
iamjjanga-ouo / dataclass.py
Created February 23, 2021 00:10
Python: @DataClass decoration example
# python 3.7+
from dataclasses import dataclass
@dataclass
class Product:
weight: int = None
price: float = None
apple = Product()
apple.price = 10
@iamjjanga-ouo
iamjjanga-ouo / generator.py
Created February 23, 2021 00:58
Python: Generator
def get_natural_number():
n = 0
while True:
n += 1
yield n
get_natural_number() # <generator object get_natural_number at 0x10d3139d0>
g = get_natural_number()
for _ in range(0, 100):
@iamjjanga-ouo
iamjjanga-ouo / go_cwd.go
Created March 16, 2021 02:33
Go: get current working directory
package main
import "os"
func main() {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
println(dir)
@iamjjanga-ouo
iamjjanga-ouo / go_echo.go
Last active March 19, 2021 08:28
GoWeb: echo: echo, echo.GET(), echo.Logger
// Golang with Echo - starting Echo
// echo
// - echo는 엄청 큰 struct이다.
// - e := echo 여기서 e는 echo구조체를 가리키는 pointer
// e.GET(path, handleFunc, ...)
// - HandleFunc(context) 현재 request의 context이다. -> interface
// e.Logger.Print() / e.Logger.Fatal()
// Logger defines the logging interface.
@iamjjanga-ouo
iamjjanga-ouo / main.go
Last active March 19, 2021 08:29
GoWeb: basic: net/http ListenAndServe
// ListenAndServe(addr string, handler Handler) error
//
package main
import (
"fmt"
"net/http"
)
type MyWebserverType bool
@iamjjanga-ouo
iamjjanga-ouo / main.go
Last active March 19, 2021 08:30
GoWeb: basic: install gin(hot reload module)
// Gin?
// gin is a simple command line utility for live-reloading Go web applications.
// install : `go get github.com/codegangsta/gin`
// usage : gin --all -i run main.go
package main
import (
"fmt"
"net/http"
@iamjjanga-ouo
iamjjanga-ouo / main.go
Last active March 19, 2021 08:30
GoWeb: basic: HandlerFunc(f)
//HandlerFunc(f)
// The HandlerFunc type is an adapter to allow the use of ordinary functions as HTTP handlers.
// If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.
package main
import (
"fmt"
"net/http"
)
@iamjjanga-ouo
iamjjanga-ouo / main.go
Created March 19, 2021 08:37
GoWeb: basic: HandleFunc
//HandleFunc
// [!] HandlerFunc와 HandleFunc는 이름이 비슷하지만 다른함수이다.
// func HandleFunc(pattern string, handler func(ResponseWriter, *Request))
// - localhost:8080/hello와 localhost:8080/login 그리고 각 endpoint에 대해서 GET, POST, DELETE 등 각각 다르게 동작해야한다.
// - 하드웨어 상이나, 다르게 Multiplexer를 이용하지만, echo Framework를 사용하기전 간단하게 HandleFunc로 구현해본다.
package main
import (
"fmt"
@iamjjanga-ouo
iamjjanga-ouo / main.go
Created March 19, 2021 08:39
GoWeb: basic: Handle (Handle vs HandleFunc and using Handle type with variable)
//Handle
// - Handle과 HandleFunc의 차이점은 2번째 parameter로 어떤 타입을 취급하냐이다.
// func Handle(pattern string, handler Handler)
// func HandleFunc(pattern string, handler func(ResponseWriter, *Request))
package main
import (
"fmt"
"net/http"
)
@iamjjanga-ouo
iamjjanga-ouo / main.go
Created March 19, 2021 08:40
GoWeb: basic: http methods, it is needed for different endpoints (GET, POST, ...)
//http methods net/http
// - endpoint로 접근을 위한 다양한 방법 (GET, POST, ...)를 가진 request를 구분하는 방법
// `r.Method`를 이용하여 처리한다.
// switch ~ case문을 이용하면 더 깔끔한 처리가 가능하다. (하지만, 복잡성에서는 선호하지 않는 방법이다.)
// Production 환경에서나, 실무 API Server를 구현할 때, switch문을 사용하면 complexity한 처리를 하지 못할 수도있다.
// 또 다른 방법으로는 different endpoint를 처리하는 Function을 두는 방법이 현명하다.
package main
import (