Importing third-party packages
Most projects will depend on third-party libraries that must be imported into them. The Go module system manages these dependencies.
How to do it...
- Find the import path of the package you need to use in your project.
- Add the necessary imports to the source files you use in the external package.
- Use the
go get
orgo mod tidy
command to add the module togo.mod
andgo.sum
. If the module was not downloaded before, this step will also download the module.
Tip
You can use https://pkg.go.dev to discover packages. It is also the place to publish documentation for the Go projects you publish.
Let’s add a database to our program from the previous section so that we can store the data submitted by the web form. For this exercise, we will use the SQLite
database.
Change the cmd/webform/main.go
file to import the database package and add the necessary database initialization code:
package main import ( "net/http" "database/sql" _ "modernc.org/sqlite" "github.com/PacktPublishing/Go-Recipes-for-Developers/src/chp1/ webform/pkg/commentdb" ) func main() { db, err := sql.Open("sqlite", "webform.db") if err != nil { panic(err) } commentdb.InitDB(db) server := http.Server{ Addr: ":8181", Handler: http.FileServer(http.Dir("web/static")), } server.ListenAndServe() }
The _ "modernc.org/sqlite"
line imports the SQLite
driver into the project. The underscore is the blank identifier, meaning that the sqlite
package is not directly used by this file and is only included for its side effects. Without the blank identifier, the compiler would complain that the import was not used. In this case, the modernc.org/sqlite
package is a database driver, and when you import it, its init()
functions will register the required driver with the standard library.
The next declaration imports the commentdb
package from our module. Note that the complete module name is used to import the package. The build system will recognize the prefix of this import declaration as the current module name, and it will translate it to a local filesystem reference, which, in this case, is webform/pkg/commentdb
.
On the db, err := sql.Open("sqlite", "webform.db")
line, we use the database/sql
package function, Open
, to start a SQLite
database instance. sqlite
names the database driver, which was registered by the imported _ "modernc.org/sqlite"
.
The commentdb.InitDB(db)
statement will call a function from the commentdb
package .
Now, let’s see what commentdb.InitDB
looks like. This is the webform/pkg/commentdb/initdb.go
file:
package commentdb import ( "context" "database/sql" ) const createStmt=`create table if not exists comments ( email TEXT, comment TEXT)` func InitDB(conn *sql.DB) { _, err := conn.ExecContext(context.Background(), createStmt) if err != nil { panic(err) } }
As you can see, this function creates the database tables if they have not been created yet.
Note the capitalization of InitDB
. If the first letter of a symbol name declared in a package is a capital letter, that symbol is accessible from other packages (i.e., it is exported). If not, the symbol can only be used within the package it is declared (i.e., it is not exported). The createStmt
constant is not exported and will be invisible to other packages.
Let’s build the program:
$ go build ./cmd/webform cmd/webform/main.go:7:2: no required module provides package modernc.org/sqlite; to add it: go get modernc.org/sqlite
You can run go get modernc.org/sqlite
to add a module to your project. Alternatively, you can run the following:
$ go get
That will get all the missing modules. Alternatively, you can run the following:
$ go mod tidy
go mod tidy
will download all missing packages, update go.mod
and go.sum
with updated dependencies, and remove references to any unused modules. go get
will only download missing modules.