in Education by
I've created a library as the module for personal use outside of "GOPATH" in "database" folder with this command "go mod init database," and I don't know: How to use/import this module in another module? OS: Windows 7, Go: v1.11 JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The easiest and working out-of-the-box solution is to put your database package / module into a VCS (e.g. github.com), so other packages (inside other modules) can simply refer to it by importing it like: import "github.com/someone/database" If you do so, you don't even have to fiddle with the go.mod files manually, everything will be taken care of by the go tool: it will automatically recognize and resolve this dependency, download and install the required package, and will also update go.mod automatically. Staying entirely on local disk If you don't want to use a VCS (e.g. you're just experimenting or you haven't decided what to use yet), then you can still do it. The how is detailed in the official Go Wiki: Can I work entirely outside of VCS on my local filesystem? So you created a database folder outside of GOPATH, and you created a module in it. And you created another module, let's call it main, and you want to use this database package. What you must do is: go.mod of your main module must list the database package as a "requirement". Give a temporary VCS name to your database package: require ( example.com/me/database v0.0.0 ) You must tell the go tool where this package is located, because the full package name we used is just a temporary / fantasy name. Use the replace directive to make this database package point to a folder on your local disk; you may use absolute and relative paths: replace example.com/me/database => ../database And that's all. Working example Let's see a working example. Let's create a pretty module. Create a pretty folder with 2 files in it: pretty.go: package pretty import "fmt" func Pretty(v ...interface{}) { fmt.Println(v...) } go.mod (can be created by running go mod init pretty): module pretty Now let's create another, main module. Let's create a folder osinf (it may be whatever) next to the pretty folder. 2 files in it: osinf.go (note we intend to use our pretty package / module, we import it by "example.com/me/pretty"): package main import "example.com/me/pretty" func main() { pretty.Pretty("hi") pretty.Pretty([]int{1, 3, 5}) } go.mod: module main require example.com/me/pretty v0.0.0 replace example.com/me/pretty => ../pretty And that's all. Running go run osinf.go in the osinf folder, the output is: hi [1 3 5]

Related questions

0 votes
    What is the most idiomatic way to handle multiple errors in go? Should I try to wrap the error ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Is it possible to get model's table name? I see that it's possible to get it from ModelStruct ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I want to execute HelloWorldConsole.exe with an shipped Mono Framework through Go. So I want to call mono- ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I have two modules. App and module Test. Module app contains MainActivity with button. Module Test contains ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 30, 2022 in Education by JackTerrance
0 votes
    The indirect change of the values of a variable in one module by another module is called (a) Internal ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    My git workspace is dirty, there are some local modifications. When I use the command git pull origin master it ... Ansible git module? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    How can we use SAP MM or Material Management module?...
asked Mar 15, 2021 in Technology by JackTerrance
0 votes
    Which module you can use to install Apache in Ubuntu OS? 1. yum 2. apt...
asked Jan 27, 2023 in Technology by JackTerrance
0 votes
    Which module will you use to create a directory? (i)file (ii)fetch (iii)copy (iv)template...
asked Jan 24, 2023 in Technology by JackTerrance
0 votes
    Someone (from outside) who tests security issues for bugs before launching a system or application, and who is not ... questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 5, 2021 in Education by JackTerrance
0 votes
    A filter that passes all frequencies lying outside a certain range, while it attenuates all frequencies between the two ... EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    ____________ is the NiFi element that is utilized to monitor incoming data, pull data from outside sources, publish data ... the options (2)Connection (3)Processor (4)Relationship...
asked Apr 18, 2021 in Technology by JackTerrance
0 votes
    I've got an extensive project. Amongst other things, it contains an UserForm Worksheet (Data) with a button ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    I'm working on a mini text role playing game where you must survive dinner with the king. I'm ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 8, 2022 in Education by JackTerrance
0 votes
    ActiveRecord 3.2.14 I want to use ActiveRecord in a non-Rails Ruby project. I want to have ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
...