2

I have the following code, is really simple. The point is that the code after the first fmt.Println is never executed. Any idea why?

The code creates a random string, and then creates a Gin Router. The code that executes the router is never being runned.

func send(cmd *cobra.Command, args []string) {

    randomString = createRandomString()
    fmt.Println("Code for share: " + randomString)
    var files filesToSend = args

    //Create http to listen to port
    g := gin.Default()
    g.GET("/", files.sendHttpHandler)
    g.Run()
}
  • How do you know it's never executed? How are you invoking `send`? Where's `main`? – mkopriva Dec 09 '20 at 08:39
  • The whole code is [here](https://github.com/mariogmarq/GoShare). The server is not being created(or at least it doesn't report that is running, which should be doing since is not in production mode). Also putting any other Println command after that one won't work. Anything runs after that println – Mario García Márquez Dec 09 '20 at 08:44
  • Try adding `panic("exit")` before `g.Run()` instead of a plain `fmt.Println`, does the code reach it or does it not? – mkopriva Dec 09 '20 at 08:57
  • Nope, it doesn't panic. Should I create an issue in github? – Mario García Márquez Dec 09 '20 at 08:58
  • Your module path and import paths do not match, every invocation of the command is using probably a cached "dependency" that is not the actual "live" source. – mkopriva Dec 09 '20 at 09:10
  • i.e. `GoShare` is not `goshare`. – mkopriva Dec 09 '20 at 09:10
  • remove this: https://github.com/mariogmarq/GoShare/blob/master/go.mod#L7 and fix case in this: https://github.com/mariogmarq/GoShare/blob/master/main.go#L18 – mkopriva Dec 09 '20 at 09:11
  • it worked!! Thank you!! Didn't know that Golang cached dependencies – Mario García Márquez Dec 09 '20 at 09:26
  • I believe only those dependencies that are remote to the project, and because the import in your main did not match up with the module's path, go went ahead and downloaded the source for `...goshare/cmd` from github and cached it. When the paths match, then the dependency is local and is not cached but instead used "directly". – mkopriva Dec 09 '20 at 09:32
  • Go itself caches dependencies in the [modcache](https://golang.org/ref/mod#module-cache). – TehSphinX Dec 09 '20 at 09:47

1 Answers1

2

Problem is the import path in main.go vs the module name in go.mod. The capitalization is different:

package main

import "github.com/mariogmarq/goshare/cmd"

go.mod:

module github.com/mariogmarq/GoShare

It is best practice to use all lowercase for package (and module) names. From the Go Blog:

Good package names are short and clear. They are lower case, with no under_scores or mixedCaps.

TehSphinX
  • 6,536
  • 1
  • 24
  • 34