[go] How to call the class in other package in Go Programming Language

Assume we have two file, of two packages. Here i will use web.go extension to run this example.

These two files are located in the same folder.

test.go

package main

import (
"web"
"./user"// Please pay attention here
)

func hello(val
string) string{
return "Hello, world"
}

func main () {
web.Get(
"/(.*)", user.Create)
web.Run(
"0.0.0.0:9999")
}

user.go

package user

func Create(val
string) string{
return "create"
}

func Hello(val
string) string{
return "Hello, world"
}

The steps to run the app.

1. Compile the user.go first!

8g user.go

2. Compile and run the test.go

8g test.go
8l test.
8
.
/8.out

Here are two steps you have to be aware of:

A. How to import the user package ==> import ("./user")

B. First compile the user.go!

原文地址:https://www.cnblogs.com/davidhhuan/p/1860429.html