3 ms·
``` package main import ( "fmt" "log" "net/http" ) func init() { setupDBConnection(dbName) } func handler(w http.ResponseWriter, r *http.Reques
by justicezyx 6y ago
```
package main
import (
"fmt"
"log"
"net/http"
)
func init() {
setupDBConnection(dbName)
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func main() {
fmt.Println("Processing your serverless request")
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
```
This code snippet is an example of "Run bootstrapping logic once, and reuse it across Min Instances".
Assuming the boostrapping logic refers to the `init()` function that connects to DB. Does Cloud Run looks for init() in a Golang app to invoke?
I cannot seem follow the code's logic and its connection with the stated purpose.
Did I miss anything?
- yegle 6y ago`init()` is part of Golang spec: https://golang.org/ref/spec#Package_initialization https://golang.org/ref/spec#Package_initialization