-1

my directory set up is like below

    .
├── go.mod
├── main.go
└── urlshort
    └── urlshort.go

go.mod

module fromscratch_urlshort

go 1.22.3

root dir name is fromscratch_urlshort, I did
go mod init fromscratch_urlshort

top of urlshort.go is

package urlshort

in main.go

I do

import(
  "urlshort"
)

when I run code I got error saying go run main.go main.go:6:2: package urlshort is not in std (/usr/local/go/src/urlshort)

What am I doing wrong here ? My objective is to import the local module urlshort in main.go

P.S. What is a good way to set up go module when pulling a go project from a repo say github ? would the pulled directory work with go mod init ? what needs to be done ?

3
  • I figure out the import problem, still would like advices on the starting module after pulling go code remotely tho, so please share insights ! Commented Aug 10, 2024 at 23:08
  • so my P.S. question refers to pulling a whole remote golang project and start working with it, often there's no go.mod , so how to start with setting the module up ... Commented Aug 10, 2024 at 23:17
  • @user2600411, if the repo has no go.mod it is not a complete go project and you’ll have to fix whatever is missing. Set things up just like you would any other project, following the official docs. Commented Aug 11, 2024 at 3:45

1 Answer 1

1

I figured out. The import line in main.go should be

import (
  "fromscratch_urlshort/urlshort"
)

as fromscratch_urlshort is the module name.

It's crazy how much time I spent on this.

Sign up to request clarification or add additional context in comments.

3 Comments

This was the most frustrating part for me with learning golang was how do packages get names. It's so much different than in python. go.mod interacts in weird ways. Where does the go.dev proxy come into play, etc!
Thank you for sharing ! I definitely spend more times I wanted to deal with this (spoiler: every time lol) , it really comforts me that maybe it's not just me not able to figure this out.
It's also confusing cause a file path doesn't necessarily mean that's the package that is imported. By that if you import 'github.com/u/foo' you might not get foo in the namespace it could be named something else. Or at least you can name it something.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.