I have forked and cloned this repo(GitHub - AdminTurnedDevOps/go-webapp-sample: golang, echo, gorm) in my local windows machine and I was just curious to run the app in my machine. I’m also new to go and never introduced to mod, packages, rest api etc. However, when I try to run locally in my VSCode app, it pushes this error in terminal. No issues in kodekloud Labs whatsoever. I verified the files config.go, repository.go etc… no changes with mine. This is blocking my hands on with my machine
I have found the solution myself.
Please note if you are receiving the error below while running the code on local machine(mine is Windows) please follow the below steps:
Error in terminal:
ERROR logger/gormlogger.go:41 [gorm] failed to initialize database, got error %v, E:/Learn/KodeKloud/Golang/go-webapp/go-webapp-sample/repository/repository.go:80%!(EXTRA *errors.errorString=Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub)
screenshot:
Solution:
Step 1.
if you try to understand on highlevel it basically says for gorm to run you need “CGO_ENABLED=1”,
for this to setup you need to do “export CGO_ENABLED=1
” or “set CGO_ENABLED=1
”. These all depends on the shell you use. In windows I use powershell and the command to set it is “$env:CGO_ENABLED = 1
”. if in case you get any permission issue, run powershell as admininstrator.
Step 2) you will receive this “cgo: C compiler “gcc” not found: exec: “gcc”: executable file not found in %PATH%”
for this, you need to do below.
go to tdm-gcc (jmeubank.github.io), download and install the gcc from here.
P.S. This will automatically push the path to env variable. if not, please add it here like this
it works for me, thanks a lot