3. GO-Project structure and package access permissions

3. GO-Project structure and package access permissions

3.1. Project structure in goland

(1) Create a standard Go project in goland

(2) Goland configuration

Create project Learn-Go

file-settings-go-GOPATH-add

Create the src directory in the project directory, create the demo directory in the src directory, and create the demo.go file in the demo directory

//Learn_Go/src/demo/demo.go
package demo

import "fmt"

func Demo() {
	fmt.Println("Execute demo")
} 

 Create the main.go file in the project directory

//Learn_Go/main.go
package main

import "demo"

func main() {
	demo.Demo()//Execute demo
}

 Directory results

Right click the project-->>run-->>go_build Learn_Go

3.2. Package and package level access permissions

 Lowercase in the same package can be accessed, other packages must be uppercase if you want to access

Directory results

 demo1.go

//Learn_Go/src/demo/demo1.go
package demo

import "fmt"

func demo1() {
	fmt.Println("Execute demo111")
}

demo2.go

//Learn_Go/src/demo/demo2.go
package demo

import "fmt"

func Demo2() {
	fmt.Println("Execute demo222")
	demo1()//Can be accessed in the same package, no capital required
}

main.go

//Learn_Go/main.go
package main

import "demo"

func main() {
	demo.Demo2()
}

//result
Execute demo222
Execute demo111
Reference: https://cloud.tencent.com/developer/article/1481009 3.GO-Project structure and package access permissions-Cloud + Community-Tencent Cloud