package main
import (
"fmt"
)
const maxIdx = 16 // これより大きいとオーバーフローする
func frac(n int) int {
if n == 0 {
return 1
}
return n * frac(n-1)
}
func expotensial(x int) int {
base := 1
for i := 1; i <= x; i++ {
base = base * x
}
return base
}
func main() {
for i := 0; i < maxIdx; i++ {
fmt.Printf("%d! = %d, %d^%d = %d\n", i, frac(i), i, i, expotensial(i))
}
}
json [Golang] golang #golang #snippets中有用的片段
[ fmt ](https://golang.org/pkg/fmt/)
-------
Print any `type`(struct,maps,primitives) with the `key` name
```
fmt.Printf("\n [key] :%+v \n", [value])
```
Print Error
```
fmt.Errorf(" \nError: %s", err.Error())
```
[ log ](https://golang.org/pkg/log/)
-------------
Print any `type`(struct,maps,primitives) with the `key` name
```
log.Printf("\n [key] :%+v \n", [value])
```
[ http ](https://golang.org/pkg/log/)
-------------
http middleware
```
func [middlewareName](h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Do things before request
nextMiddleware.ServeHTTP(w, r)
// Do things after request
})
}
```