GoLand different directories (package) method call

The novice learns go, call the directory on cross is really difficult to get a few days, almost gave the go in this catalog show you how to cross (package) call ~

 

Main.go call is in demand mysql.go Query method in the model package, the following directory structure.

 

 

 

Highlights:

1. Create a new project when the need to use a domain name path (signed ''), such as test.cn, if ordinary without 'point' when the given name will GoLand reference.

2. Set Enable "Enable Go Modules"

3. Do go mod init in the directory with the project

4. Bring the project name calling, such as import "test.cn/model"

 

specific method:

1) project name using the domain name, to create a new test.cn, or right Refactor has been built on the directory, folder names and project names have changed.

2) Setting the setting is enabled go module (no need to set other go path and the like, using the default), the path: Go - Go Modules (vgo), check the Enable Go Modules, uncheck vendoring or there will be other problems.

 

 

3) found in the bottom left of the editor "Terminal" tab, do go mod init command to enter, suggesting creating new go.mod be the successful

4) calls import "test.cn/model", pay attention to the method name to be called the first letter capitalized Oh!

 

Two file code is as follows:

main.go

package main

import (
	"fmt"
	"test.cn/model"
)

func main(){
	was result string
	result = model.Query()
	fmt.Println(result)
}

mysql.go

package model

func Query() string{
	return "Joe Smith"
}

  

Execution results are as follows

 

 

 

import (
"fmt"
"test.cn/model"
)

Guess you like

Origin www.cnblogs.com/wurijie/p/12006117.html