惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
爱范儿
爱范儿
V
Visual Studio Blog
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 三生石上(FineUI控件)
博客园 - Franky
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
罗磊的独立博客
L
LangChain Blog
Jina AI
Jina AI
IT之家
IT之家
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页

博客园 - moonz-wu

招聘研发工程师 职业规划:管理vs技术 PacificA中的租约与失效检测解读 可扩展的事件复用技术:epoll和kqueue 面试的,或许用的着 Zero to One读后感 Fourth glance in Go Third glance in Go Second glance in Go 好RESTful API的设计原则 RESTful API的设计与开发 架构风格之比较 所有有意愿成为程序员的人都应该看的文章 马不停蹄的向前走着,却发现羊已经等在了前面-我的2014 Hadoop从伪分布式到真正的分布式 看到一篇很有意思的文章,但是我们身边确实有这样的人 如何延长windows评估版的方法 关于技术的选择,作为开发人员你应该看看 新的一年又开始了
First glance in Go
moonz-wu · 2015-01-27 · via 博客园 - moonz-wu

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 math

func Add(a, b int) int {
    return a + b
}

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

package main

import (
        "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 main

import (
        "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 math

func Sub(a, b int) int {
    return a - b
}

func AddASubB(a, b int) int {
    c := Add(a, b)
    return c - b
}

//-- example2.go ---
package math

func 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/