博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
First glance in Go
阅读量:6849 次
发布时间:2019-06-26

本文共 2817 字,大约阅读时间需要 9 分钟。

Because I forgot to install the Chinese input in this OS, I have to use English input.

The first problem which I ran into is "how to import the local file in Golang"

First, it may be my fault but I really didn't see any documents say that if you want to import a local file in another Go file, you should create a folder to put the imported file. Below is my case:

I created a file said "example2.go", then typed some codes:

package mathfunc Add(a, b int) int {    return a + b}

I wanted to import it in another file said "example1.go"

package mainimport (        "fmt"        "./example2"       )func main() {    fmt.Println("Hello Go!")    fmt.Println(math.Add(1,2))}

when I tried to run the example1.go file, the go runtime threw an error said "couldn't find the example2". what's the problem? Is GOPATH not set? Is example2.go not compiled? Finally, I found it is the folder structure problem. My orignal folder structure likes this

Goworkspace- pkg- src    - example1.go    - example2.go- bin

But Go 1.4 doesn't support import a single file as a package. So I have to put the "example2.go" in a folder said "package2"

Goworkspace- pkg- src    - example1.go    - package2        - example2.go- bin

then change the code

package mainimport (        "fmt"        "package2"       )func main() {    fmt.Println("Hello Go!")    fmt.Println(math.Add(1,2))}

it works. So it means a package always refers to a folder in the Golang. But you may notice that the package name isn't same as the folder name. In Golang, when you use import keyword to import a package. There is two things will happen

1. Golang to find the package file location

2. Parse the package structure for using

So, the first thing is you should use a folder to indicate the package file location. Then you can go to use package by name.

Another thing is a package can accross multiple files, it looks like below

Goworkspace- pkg- src    - example1.go    - package        - example2.go        - example3.go-bin

The example2 and example3 files are under the same package. Below is the codes

//-- example3.go--package mathfunc Sub(a, b int) int {    return a - b}func AddASubB(a, b int) int {    c := Add(a, b)    return c - b}//-- example2.go ---package mathfunc Add(a int, b int) int {    return a+b}

 You may notice AddASubB function invoked the example2.go's Add function, but I didn't import anything. Yes, if the function under the same package, you can invoke it directly no matter it's export or not. But the most import thing is the package must be imported in some where. If you write a file said example4.go under the main package, then you want to invoke the function in exampl1.go file. It will be failed. Because no place to trigger the import main package operation.  

 

这里有篇文章讲Go包的管理机制:http://io-meter.com/2014/07/30/go%27s-package-management/

 

转载地址:http://gtrul.baihongyu.com/

你可能感兴趣的文章
C#根据句柄改变窗体控件值
查看>>
Beam编程系列之Python SDK Quickstart(官网的推荐步骤)
查看>>
Dropping TSO features since no CSUM feature
查看>>
java项目中通过添加filter过滤器解决ajax跨域问题
查看>>
phalcon的CLI应用
查看>>
用SVN checkout源码时,设置账号
查看>>
Linux命令及架构部署大全
查看>>
chrome插件开发之调试
查看>>
java 面试
查看>>
如何获取用户的地理位置-浏览器地理位置(Geolocation)API 简介
查看>>
五种常见软件架构
查看>>
NavigationViewDemo【和DrawerLayout搭配使用实现侧滑导航视图界面】
查看>>
Redisson分布式锁实现
查看>>
[PWA] Customize the Splash Screen of a PWA built with create-react-app
查看>>
Oracle EM错误,java.lang.Exception: Exception in sending Request :: null ...
查看>>
算法战斗:给定一个号码与通配符问号W,问号代表一个随机数字。 给定的整数,得到X,和W它具有相同的长度。 问:多少整数协议W的形式和的比率X大?...
查看>>
20140704,七月微软安全补丁的通知
查看>>
Java 多线程(2)-Executor
查看>>
解决Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题...
查看>>
【探索】在 JavaScript 中使用 C 程序
查看>>