`go run main.go` returns: `import ".../cmd" is a program, not an importable package`

I’m new to Go. I’ve browsed around on this issue but can’t find anything with a solution that applies to my case. Code available at GitHub - neut0ne/todo: Simple task manager to learn Go..

I try to run a program. It has 2 files in use, main.go and cmd/root.go.

This is main.go:

package main

import "github.com/neut0ne/todo/cmd"

func main (){
  cmd.RootCmd.Execute()
}

This is cmd/root.go:

package cmd

// This is the root command.

import "github.com/spf13/cobra"

var RootCmd = &cobra.Command{
  Use:   "Todo",
  Short: "Todo is a small and efficient task manager.",
//  Long: `A Fast and intuitive basic task mamagement tool built with
//                love by neut0ne and friends in Go.
//                Complete documentation is available at http://todo.neut0ne.go`,
//  Run: func(cmd *cobra.Command, args []string) {
//    // Do Stuff Here
//  },
}

I’m told you’re supposed to be able to go mod init ~/todo to create a module for the program,
and then be able to just type todo to run the program.
todo gives nothing.
Standing in ~/todo locally and typing go run main.go gives the error message below:

main.go:3:8: import “github.com/neut0ne/todo/cmd” is a program, not an importable package

I have run go install root.go and go install cmd.
GOPATH: ~/code/go/src/github.com/neut0ne/todo
go version go1.14.2 darwin/amd64

What am I doing wrong?

I found a workaround. Naming /cmd to something else magically transformed it into a package and let me run the program. I named it /cmdd.
But I still can’t understand why /cmd is defined as a program and not a package for me. I’ve seen so many other go repos using /cmd. I’d be delighted to take part of an explanation if anyone knows why this happened.

https://github.com/neut0ne/todo/cmd does not exist. Can your share your project in the form that generated the error?

sorry, the correct link would be: https://github.com/neut0ne/todo/tree/master/cmd

I don’t think I can reproduce the error. Now it works with cmd. I added a new command using cobra, which automatically generates that command in a /cmd directory in root, with some standard formation.

Still no idea why cmd didn’t was interpreted as a program and not a directory in the first place. I am happy with this, I’ll close the issue.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.