SoFunction
Updated on 2025-03-01

go module import The method of calling local module tidy

How to call a local module you wrote

  • $go mod init in the project root directory any name (such as /cde)
  • Write a local module, import the module (/cde/path of the module) in other files and use it

Other notes:

  • Do not write main in the package name in the called module, such as packge abc, then when other packages call it, it is called abc (you can see in vscode that is equivalent to import the package as abc)
  • The called module can not write the main method
  • The called module's directory at the same level can have multiple go files, but cannot have the same method name. If there is a B1 method in b1\, there is no B1 method in b1\.
  • b1\b1inner\ and b1 are different packages. So there is a B1 method in b1\, and it can also be in b1\b1inner\
  • There must be a file, and you must write demo666 in the module demo666 at the beginning of this file when importing it. This name can be taken arbitrarily when go mod init, and can be modified later
//demo1\b1\
package b1
import (
	"fmt"
	"demo666/a2"
)
func B1() {
	("B1")
	a2.A2()
}
//demo1\b1\
package b1

import (
	"fmt"
)
func B2() {
	("B2")
}
//demo1\b1\b1inner\
package b1i

import (
	"fmt"
)
func B1() {
	("b1inner")
}
//demo1\util\
package dbpackage

import (
	"database/sql"
	"fmt"
	_ "/lib/pq"
)
func Select(db *) {
	rows, err := ("SELECT * FROM users where id in (3,44,45,46,47,48,49)")
	CheckError(err)
	var es []Product
	for () {
		var e Product
		(&, &, &)
		es = append(es, e)
	}
	("%v", es)
}

*******************************
//demo1\a2\
package a2

import (
    "fmt"
)

func A2() {
	("A2")
}
//demo1\
module demo666
go 1.19
require /lib/pq v1.10.7
//demo1\
package main
import (
	"demo666/b1"
	"demo666/b1/b1inner"
	"demo666/util"
	"fmt"
)
func main() {
	("Starting")
	b1.B1()
	b1.B2()
	db1 := ()
	defer ()
	(db1)
	b1i.B1()

}

The output is as follows
Starting
B1
A2
B2
[{44 apple 99} {45 apple 99} {46 kitty1 1} {47 kitty2 2} {48 kitty3 0} {49 kitty1 1}]
b1inner

go mode tidy:
Refer to the dependencies required by the project to the file, and remove the dependencies not required by the project in the file.

References

/p/7675b8923878

This is the article about go module transformation import calling local module tidy. This is all about this article. For more relevant contents of go module local modules, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!