List below contains some useful information and can be very beneficial while working with database driven Go application so it’s up to you to test and apply to your own code. The most of the information below is coming from  Go database/sql tutorial and  The Ultimate Guide To Building Database - Driven Apps with Go documents.

The most important thing you will learn here and must take care of is the usage of db.Prepare function. It can really hit you very very hard if you don’t follow the suggestions, don’t understand what actually it does and how it does! On top of the information written below, I highly suggest you to read second link above because there are more in-depth explanations on why.

1) Prepared vs non-prepared statements

If you are 100% sure that the data you are about the use as part of your SQL queries (insertupdatedelete) is safe (SQL injection free), you should use db.Exec instead of relying on db.Prepare statement. The reason is because, whether you have an argument placeholder in the query or not, prepared statements will waste resources by issuing three queries (PrepareExecute and Close) just to do one job whereas directly executing statements will issue only one query (Query). The example below is meant to insert a single row into a table. The prepared option will triple the number of network round-trips. However, the direct execution option will cause one network round-trip. Note: If you wish to prepare parameters explicitly, fmt.Sprintf() with the %q  placeholder could be used - e.g. fmt.Sprintf(INSERT INTO table (name, age, active) VALUES (%q, %d, %t))

#go #mysql #golang

Database Driven Golang Applications and Suggestions
1.65 GEEK