How to compile when we use more than one go files?

Hi all,
I like Go language design very much. Though it lacks OOP, its very simple to learn and use. So i just tried some code in Go and i think i want to learn this more. But i faced an obstacle in compiling when i used two Go files in my test project.
I create a testFile.go in my E:\Go Projects\Samples\Sample1 folder. I wrote a function named “sayHello” inside that file. On the top of the file, i wrote “package testFile”
Then i created a caller.go file in the same directory. And on the very first line of this file, i wrote “package main”. And in the import section, under “fmt”, i wrote “testFile”. Then in the main function, i called the “sayHello” function from “testFile.go”. But when i compile this “caller.go” It gave me this error message.
‘’’--------------------------------------------------------------------
cannot find package “testFile” in any of:
c:\go\src\testFile(from $GOROOT)
C:\Users\UserName\go\src\testFile(from $GOPATH)
‘’’----------------------------------------------------------------------------
It seems that Go is not detecting the file in the source directory of main file. How to fix this. Please help me. Thanks in advance.
Note : I have compiled this caller.go in VS Code. (Using the run button in vs code. )

2 Likes

Hi all,
I think i found the answer myself.

  1. Use “package” main in all your code files. ( I have used file names here. ) So every go file’s first line should be “package main”
    2.No need to import the other files in your main file. ( I wrote an import statement before)
  2. To run your file, just type “go run .”. You see this ? last part is a dot. Don’t miss it.
  3. To build your code an exe - “go build .” Here is also a dot. But unfortunately, i can’t see any option to tell the exe file name. By default, it is same as the folder name. So i get a src.exe instead of caller.exe. Don’t worry. we can rename it later.
    But the main problem is, i can’t run this go file with VS Code’s run button. Since it is pre-fixed to run “go run $filName”
    Anyway, i am happy that i can continue coding in Go. Otherwise, i was planning to quit Go.
2 Likes

Hi,
Thanks for the reply.
In almost all examples, it seems that people uses :github.com" as a path. As a windows user, i am using paths like “C:\FolderName\SubFolderName\FileName.Ext” C can be replaced with “D, E, F” Thats a normal path in windows. But i cant see any examples like that in go.
Please tell me how to place files when i am going to make a gui library in Go.
Assume that i need each file for each control. Like, one file for Button control, one file for TextBox control etc.
And there is a single file which imports and exports all these files. Say, it is named as “GuiLib.go”
So i am assuming that, when i need to make a windows gui program, i can easily import this “GuiLib.go” and everything will be fine. Is that true ?

2 Likes

Thats the package path, or at least a part of it.

Well, your operating system doesn’t matter, package pathes are not directly mapped to your filesystems pathes, but to a common prefix, the so called GOPATH, or if you use modules, its otherwise managed for you.

1 Like

Hi,
Thanks for the reply.
I have some experience with FreeBasic and Nim. In these two, i have created a gui library last year. All import statements are written in one file and we can call that file “GuiLib”. The library user only need to import the “GuiLib”. They can easily work with the gui class and methods. How can i do this in Go ? If i write this library as package (Lets call “GuiPack”), then does Go detect this package if i place this files on the same directory where my main.go resides ?

2 Likes

Hi,
Thanks for the reply.
Well, i have a different folder tree.
Go env = GOENV=C:\Users\UserName\AppData\Roaming\go\env
GOPATH=C:\Users\UserName\go
GOROOT=c:\go

But my project directory is different.
E:\Programming Folder\Go Projects\Samples\Sample1\sampleCode1.go
See, The folder named Sample1 contains the go code files.So i am expecting go to find the package files from this source directory. But it cant do this.

2 Likes

This is totally fine if you use go modules, a feature introduced with go 1.11 and slightly rewritten in 1.12.

You need to have a go.mod file in your project root to make this work. If you do not want to, or can’t use go modules, you need to put your project under $GOPATH/src, as this is how Go works.

2 Likes

Basically, i don’t want to keep my project files in C Drive. C drive is for install all programs and compilers including go. I want to keep all my project files inside a dropbox folder, so that i can enjoy learning in my pc while i am working and in my laptop while i am in home.
So please tell me how to fix go’s path problem.

2 Likes

I dont get you ? Is that sarcasm ?

1 Like

As I said, use Go Modules. Or change your GOPATH to point whereever you like.

2 Likes

So you mean, each time when i am going to create a new project in Go, i can change the go path with this bat file ? Then let me ask you one thing ? Is there any IDE which will do this for me ?

2 Likes

I am sorry to say this, but i really dont know what is a module in Go. In vb.net, a module is a static class. You can write code in a module, and you dont worry about the import business, because the ide will do it for you.

2 Likes

A “module” in Go is a collection of packages under a common “namespace”. Sadly I can not give you any equivalences in the VB.NET area, as I am not used to the dotNET ecosystem.

I gave you a link to the official documentation of the modules though.

Basically, I’d say, that a Go Module is what other languages would call a project.

I know that Visual Studio Code can deal with a GOPATH as the project setting, through the .vscode/settings.json file or how it was called. Though this would only affect the package searchpath for the IDE and loaded tools, also then you need to add a global setting for the “tools GOPATH” that they get installed only once in a separate area. If you miss that step, the Plugin will install the tools for each and every project in its local GOPATH.

And still you will need to set the GOPATH for the shell session yourself, if you want to use a shell.

Though I’d advice against messing around with the GOPATH, either use it, as it is designed and use a single GOPATH for all your projects, or use Go Modules, which is the new and shiny approach to do projects in Go.

2 Likes

Thanks for the detailed reply. I think its better to choose the last option “Go Modules”.

2 Likes

Beleive it or not, i actually JUST FINNISHED a program to aid in this,

I made it more for Windows and the likes, but it should work without issue (if there is a issue, tell me plz)

I use mine in a portable setup with the msys2.org and Git Bash software installed on my flashdrive,
github/Merith-TK/Personal-Projects, look at launcher.go.

2 Likes

As a windows user we don’t like cmd things. We would like to click on a Run button and sit n watch the magic. But since 2017, curiosity drives me crazy to explore some new programming languages other than .net family. That’s how I came to CMD World, Usually, if we like a programming language on Windows, we have the EXE file on its website. If you download and run it, it will automatically do the environment variable setup. We don’t need to know anything. But that’s not the case with languages like Go.

2 Likes

Thanks. Let me check
BTW, an off topic question - Is there any way to change the for loop syntax in Go ? When we want to iterate thru a collection, its quiet easy if we can write like this “for x in collection” and x represent each item in that collection.

2 Likes

Thanks for the reply. But half of the matter in your reply is unknown to me. One thing is very clear, I need to learn Go modules. Have some fair knowledge about modules in D. I hope i can easily learn it.

2 Likes

Well, actual syntax for your request is for index, x := range collection { ... }

3 Likes

I actually implement this in my program

k is the first var in the loop, in this case, ENV NAME
v is the second var in the loop, in this case, what you wish to set ENV VAL

And to make your life easier to navigate, i HIGHLY reccomend using something like cygwin or msys

3 Likes