The main.go file

Now that you have a basic understanding of our project, let's take a look at the main.go file. This is the file responsible for sending settings necessary for the operation of our microservice to our app and for running the microservice itself. As can be seen in the following code, we instantiate a connection to the database from the beginning. This instance is the database that will be used in every application:

 package main import ( "fmt" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" "log" "os" ) func main() { connectionString := fmt.Sprintf( "user=%s password=%s dbname=%s sslmode=disable", os.Getenv("APP_DB_USERNAME"), os.Getenv("APP_DB_PASSWORD"), os.Getenv("APP_DB_NAME"), ) db, err := sqlx.Open("postgres", connectionString) ...

Get Microservice Patterns and Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.