Home > Blockchain >  Golang: go runs old versions of main.go after changes
Golang: go runs old versions of main.go after changes

Time:04-07

When I run main.go, code change does not appear.

func main() {
    // Commenting out below line works correctly
    // after commenting out and access to "/" path returns server error
    http.HandleFunc("/", handler.IndexHandler)

    // but
    // Commenting out below line or Add some code in the AuthGoogleHandler function
    // seems not to appear in actual behavior
    // Even after commenting out, "/auth/google" works for some reason.
    http.HandleFunc("/auth/google", handler.AuthGoogleHandler)


    http.ListenAndServe(fmt.Sprintf(":%s", os.Getenv("SERVER_PORT")), nil)
}

I have developed same url endpoints in another go projects, so I wonder if it related.

My trial is below

  • make sure I run correct main.go file
  • rebooting my PC
  • run by Goland and terminal

project structure

.
├── handler
│   ├── google.go
│   └── handler.go
├── main.go
├── oidc
│   ├── client.go
│   └── id_token.go
└── views
    └── index.html

go env results

go env                                                                                                                                                                                                                                                         !1764
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/my-user-name/Library/Caches/go-build"
GOENV="/Users/my-user-name/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/my-user-name/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/opt/go/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang  "
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/z6/zb7kgx6s513czr90q8rzlgw80000gn/T/go-build954469924=/tmp/go-build -gno-record-gcc-switches -fno-common"

Reference golang: go run is always running old code even after changes

CodePudding user response:

I found out the cause. It was browser cache problem. I run main.go and access my localhost url with chrome secret mode works correctly

Thank you for the people who answered.

CodePudding user response:

You can try a force a rebuild of all packages with the following:

go build -a 

also this can do the same:

go clean
go build
  • Related