[ent/SQLite3] insert nodes to table "users": near "RETURNING": syntax error

Problem If you use ent with sqlite3 driver in your Golang project, you may encounter the following error when you try to insert a new record to the table. insert nodes to table "users": near "RETURNING": syntax error Analysis The error message indicates that the sqlite3 driver does not support the RETURNING clause. If you are using Golang sqlite3 package github.com/mattn/go-sqlite3 in your project. The issue may be caused by the version of the package....

[Golang/VSCode] Master Golang Debugging in VSCode: Step-by-Step Guide with Delve

Introduction If you are rocking VSCode as your IDE for Golang development and haven’t installed the debugger, I would recommend you to do it now. And Don’t worry, I’ve got your back. In this friendly guide, I’m going to walk you through the process of setting up and configuring the Golang debugger in VSCode. Step by step guide 1. Install the Debugger for Golang Delve, a fantastic open-source project, is your go-to debugging companion for Golang in VSCode....

Golang timeout feature - Illustration depicting a clock and a task symbolizing the implementation of a timeout feature in Golang

[Golang] Implementing Timeout Feature in Golang for Efficient Task Processing and Error Handling

Let’s dive into an exciting scenario to explore how we can jazz up our Golang code with a cool timeout feature! Imagine this: you’re building an awesome application and you want to delegate a task to a third-party API. But here’s the catch - the processing time for this task is unpredictable. To add some spice to the mix, you decide to set a timeout for the task. If the third-party API takes too long and doesn’t return the result within the timeout, we’ll label it as error and move on, regardless of whether it eventually responds or not....

[WSL] cgo: C compiler 'gcc' not found

When I first time built a Golang project from VS Code wsl mode, I got the below error: Build Error: go build -o /home/oscar/source/github.com/xx/xx/__debug_bin -gcflags all=-N -l . # runtime/cgo cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in $PATH (exit status 2) My first reaction for this error is to install the gcc compiler in my Windows environment. However, the solution is actually to install gcc compiler in the wsl environment....

[Golang] go get: fatal: could not read Username for 'xxx': terminal prompts disabled

1. Error 1: fatal: could not read Username for 'https://github.com': terminal prompts disabled The issue appears when I tried to download a private golang repository by the command go get github.com/oscarzhou/private-helloworld. The completed error shows as below: % go get github.com/oscarzhou/private-helloworld go: module github.com/oscarzhou/private-helloworld: git ls-remote -q origin in /Users/oscarzhou/go/pkg/mod/cache/vcs/c5fb5660c8bfa54f1b957fc4e651ac76f19b939fc9015bf3bcbc21a34c925af7: exit status 128: fatal: could not read Username for 'https://github.com': terminal prompts disabled Confirm the import path was entered correctly. If this is a private repository, see https://golang....

[VSCode] module lookup disabled by GOPROXY

Sometimes when I open the Golang project with VSCode, some import packages are highlighted with error underline and the error message is like below error while importing github.com/jpillora/chisel/client: module lookup disabled by GOPROXY=off If you are sure that the packages are downloaded already, reloading the VSCode window should solve the issue.

[Golang] Code snippets - Semver comparison

To make the project backward-compatible, a common util function for comparing the semantic version is indispensable. Here is a Golang solution implemented by leveraging go-semver package. 1package utils 2 3import "github.com/coreos/go-semver/semver" 4 5// CompareSemanticVersion compares two string written in semver format 6// return 0 indicates that the first version equals to the second version 7// return 1 indicates that the first version is greater than the second version 8// return -1 indicates that the first version is less than the second version 9func CompareSemanticVersion(current, last string) int { 10 if current == last { 11 return 0 12 } 13 14 currentVersion := semver....

[Golang] Write a simple CLI program

I will show a simple example in this post. Let’s say the program called cli-example. 1package main 2 3import ( 4 "flag" 5 "fmt" 6 "os" 7) 8 9func main() { 10 command := os.Args[1] 11 os.Args = append(os.Args[:1], os.Args[2:]...) 12 13 switch command { 14 case "version": 15 fmt.Println("1.1.1") 16 } 17} The 1st argument will always be the binary name, so the command starts from the 2nd argument. If your app also needs the subcommand, then extract it from the 3rd argument, like docker image ls....

[Golang] Create Dockerfile for golang app

Containerizing the application is very popular nowadays and it makes the deployment so easy. This post will show how to create a Dockerfile for golang application. There are basically two steps in the Dockerfile: Build the golang program source code Containerize the golang app binary 1# Build source code 2FROM golang:1.18-alpine AS build 3 4WORKDIR /go/src/github.com/app 5 6COPY . . 7 8RUN go mod tidy 9RUN CGO_ENABLED=0 GOOS=linux go build -a -o main ....

[Golang] How to import a package on the specific version

In Golang, when a package is imported, by default the codebase of the package’s default branch will be used by the caller project. However, sometimes we want to import a package’s specific branch codebase. For example, the specific version fixed a bug where we can’t wait until the repository maintainer merges the pull request. So in this post, I will walk you through how to specify a package version in your Golang project....