#Go Begineer Doubt

18 messages · Page 1 of 1 (latest)

winged niche
#

I made 2 different directories in my golang project

Parent directory name (/project)

In first directory (helper) , I made one helper.go file , package name helper and i added some custom methods in this file

In second directory (encryption) , I made one main.go file , package name main and I want to use those custom methods from helper.go file,

How to import that helper package into my current main.go file
Also Please let me know that if it is mandatory to declare any go.mod file in any of the below directories

my directory structure is like this

  • project
    • encryption
      • main.go
    • helper
      • helper.go
languid folio
#

go.mod should be on the root of the folder
And then in main.go you do import "mymodule/helper"
Make sure the functions you want to use from the helper package start with an uppercase letters so they're exported

winged niche
#

Thanks @languid folio
You mean go.mod should be under /project directory like this?

  • project
    • go.mod
    • Encryption
      • main.go
    • types
      • customType.go
languid folio
#

Yeah

winged niche
#

okay, yeah its working
But when i am trying to do the same for an existing directory ( present at same level ) , it is unable to import, after syncing also its unable to import

#

Here is the project structure
In encryption directory, it is able to import that package
but in cards directory its failing

#

So i tried to remove go.mod from my cards directory, and it worked
Any reasons why this happened?

languid folio
#

I don't understand what you're doing, but as I said the go.mod should always be in the project root

#

Also you should have one package per directory, and you can't import stuff from main packages

winged niche
#

got it, I was just playing around to get my hands better on these things
So basically we should have only 1 go.mod file in the whole project , which should be under the root directory

languid folio
#

Yes

#

A module contains package
go.mod defines the module and it's dependencies.

winged niche
#

Got it
Any good resource from where i can get the deep knowledge of all the stuff? Right now I am learning from a youtube channel but he is teaching only normal coding stuff part like syntax of if-else loops, and data structures in go
I want to learn all the other basic things as well like the purpose of GOPATH, go.sum go.mod, module packages and all the other basic to basic stuff

#

It will be helpful if you can share any thread or article from where i can follow a good path to master in GO

languid folio
#

Did you read the post I linked?

winged niche
winged niche
#

@languid folio Gone through documentation from the above link