Source codes for blog series: https://www.favtuts.com/golang-go-crash-course-series/
- Download:
- Access
golang.org
redirectsgo.dev
- Download page: https://go.dev/dl/
- Install Guide: https://go.dev/doc/install
- Access
wget https://go.dev/dl/go1.20.1.linux-amd64.tar.gz
For linux:
- Remove any previous Go installation by deleting the
/usr/local/go
folder (if it exists), then extract the archive you just downloaded into/usr/local
, creating a fresh Go tree in/usr/local/go
:
$ rm -rf /usr/local/go && tar -C /usr/local -xzf go1.20.1.linux-amd64.tar.gz
(You may need to run the command as root or through sudo).
Do not untar the archive into an existing /usr/local/go
tree. This is known to produce broken Go installations.
- Add
/usr/local/go/bin
to the PATH environment variable. You can do this by adding the following line to your$HOME/.profile
or/etc/profile
(for a system-wide installation):
export PATH=$PATH:/usr/local/go/bin
Note: Changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as source $HOME/.profile
.
- Verify that you've installed Go by opening a command prompt and typing the following command:
$ go version
Confirm that the command prints the installed version of Go.
cd $GOPATH/src
mkdir golang-mux-api
cd golang-mux-api
go mod init golang-mux-api
Initialize the module for the app (from root dir)
$ go mod init github.com/favtuts/golang-mux-api
Edit the existing module file for the new name
$ go mod edit -module github.com/favtuts/golang-mux-api
To rename all imported module from OLD_MODULE to NEW_MODULE
find . -type f -name '*.go' \
-exec sed -i -e 's,{OLD_MODULE},{NEW_MODULE},g' {} \;
In our cases
find . -type f -name '*.go' \
-exec sed -i -e 's,golang-mux-api,github.com/favtuts/golang-mux-api,g' {} \;
Download external dependencies
$ go mod download
The go command uses the go.sum
file to ensure that future downloads of these modules retrieve the same bits as the first download (it keeps an initial hash)
Both go.mod
and go.sum
should be checked into version control.
$ go get -u github.com/gorilla/mux
go: added github.com/gorilla/mux v1.8.0
$ go get -u github.com/go-chi/chi
go: added github.com/go-chi/chi v1.5.4
$ go get -u cloud.google.com/go/firestore
$ go get -u github.com/go-sql-driver/mysql
go: added github.com/go-sql-driver/mysql v1.7.0
$ go get -u go.mongodb.org/mongo-driver
go: added go.mongodb.org/mongo-driver v1.11.2
$ go get -u github.com/mattn/go-sqlite3
go: added github.com/mattn/go-sqlite3 v1.14.16
$ go get -u github.com/stretchr/testify
go: added github.com/stretchr/testify v1.8.2
$ go get -u github.com/go-redis/redis/v7
go: downloading github.com/go-redis/redis/v7 v7.4.1
go: downloading gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15
go: added github.com/go-redis/redis/v7 v7.4.1
go: upgraded gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 => v1.0.0-20190902080502-41f04d3bba15
$ go get -u github.com/aws/aws-sdk-go
go: downloading github.com/aws/aws-sdk-go v1.44.235
go: downloading golang.org/x/sys v0.1.0
go: downloading golang.org/x/net v0.1.0
go: added github.com/aws/aws-sdk-go v1.44.235
go: added github.com/jmespath/go-jmespath v0.4.0
go: upgraded golang.org/x/net v0.0.0-20221014081412-f15817d10f9b => v0.1.0
go: upgraded golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 => v0.1.0
export GOOGLE_APPLICATION_CREDENTIALS='/path/to/project-private.key.json'
/home/tvt/FIREBASE/pragmatic-reviews-62551-firebase-adminsdk-l8p5o-65461f8c5b.json
From the Firebase Console: Project Overview -> Project Settings -> Service Accounts -> Generate new private key
go build
go run .
or
go run *.go
Test (specific test)
$ go test -run NameOfTest
Test (all the tests within the service folder)
$ go test service/*.go
$ export PORT="8000"
$ echo $PORT
To build docker image
$ docker build -t golang-api .
To run docker container
$ docker run -p 8000:8000 golang-api
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt-get update
sudo apt-get install redis
sudo service redis-server start
$ cd controller/
$ go test -v -run TestAddPost
=== RUN TestAddPost
--- PASS: TestAddPost (0.01s)
PASS
ok github.com/favtuts/golang-mux-api/controller 0.019s
$ go test -v -run TestGetPosts
=== RUN TestGetPosts
--- PASS: TestGetPosts (0.01s)
PASS
ok github.com/favtuts/golang-mux-api/controller 0.034s
$ go test -v -run TestGetPostByID
=== RUN TestGetPostByID
--- PASS: TestGetPostByID (0.01s)
PASS
ok github.com/favtuts/golang-mux-api/controller 0.028s
$ export PORT=8000
$ go run *.go
Mux HTTP server running on port :8000
Create new post
127.0.0.1:6379[1]> KEYS *
(empty array)
curl -v --location 'http://localhost:8000/posts' \
--header 'Content-Type: application/json' \
--data '{
"title": "Title 1",
"text": "Text 1"
}'
{"id":7515018121727868634,"title":"Title 1","text":"Text 1"}
127.0.0.1:6379[1]> KEYS *
1) "7515018121727868634"
127.0.0.1:6379[1]> GET 7515018121727868634
"{\"id\":7515018121727868634,\"title\":\"Title 1\",\"text\":\"Text 1\"}"
Scan all table items
$ aws dynamodb scan --table-name posts --profile=tvt_favtuts