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(,))
}

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(,))
}

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/

First glance in Go的更多相关文章

  1. 理解 Glance - 每天5分钟玩转 OpenStack(20)

    OpenStack 由 Glance 提供 Image 服务. 理解 Image 要理解 Image Service 先得搞清楚什么是 Image 以及为什么要用 Image? 在传统 IT 环境下, ...

  2. 解决在CentOS6.5下安装OpenStack(Icehouse版本 )出现的glance服务无法正常工作的问题

    最近一直在用Juno版本,因为项目需要,今天在虚拟机里安装了Icehouse版,其中glance组件在执行安装的过程后,出现启动失败的现象,幸好以前排查过此类错误,遂记录如下: 在官方文档(Iceho ...

  3. 4.openstack之mitaka搭建glance镜像服务

    部署镜像服务 一:安装和配置服务 1.建库建用户 mysql -u root -p CREATE DATABASE glance; GRANT ALL PRIVILEGES ON glance.* T ...

  4. Ceph与OpenStack的Glance相结合

    http://docs.ceph.com/docs/master/rbd/rbd-openstack/?highlight=nova#kilo 在Ceoh的admin-node上进行如下操作: 1. ...

  5. 探索 OpenStack 之(10):深入镜像服务Glance

    本篇博文来探讨下镜像服务Glance. 0.  基本概念 0.1 基本功能 Glance提供REST API来支持以下镜像操作: 查询 注册 上传 获取 删除 访问权限管理 0.2 Glance RE ...

  6. 【OpenStack】OpenStack系列4之Glance详解

    下载安装 参考:http://www.linuxidc.com/Linux/2012-08/68964.htm http://www.it165.net/os/html/201402/7246.htm ...

  7. 这个Glance的界面该怎么看出问题,为什么状态是SOCKT?

    这个glance的状态图有问题吗?

  8. Centos7 install Openstack - (第三节)添加镜像服务(Glance)

    Centos7 install Openstack - (第三节)添加镜像服务(Glance) 我的blog地址:http://www.cnblogs.com/caoguo 该文根据openstack ...

  9. OpenStack:安装Glance

    >安装Glance1. 安装# apt-get install glance python-glanceclient删除sqlite文件rm -f /var/lib/glance/glance. ...

  10. How a woman's glance can tell a man if she's interested

    How a woman's glance can tell a man if she's interested揭秘:女人是否喜欢你?看眼神就知道The female mind has always b ...

随机推荐

  1. C#DbHelperOleDb,Access数据库帮助类

    /// <summary> /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url=http://www.sufeinet.com/thread-655 ...

  2. 仿微博php生成短网址

    html code <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  3. Asp.net Mvc模块化开发之“部分版本部分模块更新(上线)”

    项目开发从来就不是一个简单的问题.更难的问题是维护其他人开发的项目,并且要修改bug.如果原系统有重大问题还需要重构. 怎么重构系统不是本文探讨的问题,但是重构后如何上线部署和本文关系密切.这个大家可 ...

  4. How Will Java Technology Change My Life?

    How Will Java Technology Change My Life? We can't promise you fame, fortune, or even a job if you le ...

  5. mysql命令行基本操作

    开启:打开电脑的“开始”菜单栏,找到“运行”,在运行框中直接输入:net start mysql.再 登录:Mysql  -P 端口号  -h  mysql主机名\ip -u root (用户)  - ...

  6. 动易CMS之标签管理

    一.如何添加一个标签 1.系统设置->模板标签管理->添加标签 2.输入标签名称,根据需要选择数据设置: sql语句则选择[系统数据库SQL查询] 3.添加参数 4.系统可以根据设置的条件 ...

  7. python signal信号

    作用:发送和接收异步系统信号 信号是一个操作系统特性,它提供了一个途径可以通知程序发生了一个事件并异步处理这个事件.信号可以由系统本身生成,也可以从一个进程发送到另一个进程. 由于信号会中断程序的正常 ...

  8. 关于线上的bug什么时候修复的思考

    这里系统专门指的是那种用户量大的系统,比如有几百万或者上千万的注册会员.因为小系统因为用户量少,不存在这种思考,考虑有时候是多余的.另外还有内部系统,给自己公司内部人员使用的,即便是出现了问题,也不会 ...

  9. DataSet的灵活,实体类的方便,DTO的效率:SOD框架的数据容器,打造最适合DDD的ORM框架

    引言:DDD的困惑 最近,我看到园子里面有位朋友的一篇博客 <领域驱动设计系列(一):为何要领域驱动设计? >文章中有下面一段话,对DDD使用产生的疑问: •没有正确的使用ORM, 导致数 ...

  10. 当执行sudo时提示“is not in the sudoers file”的解决方案:

    出现这种提示是由于普通用户帐号没有权限执行sudo命令,所以需要切换到root身份进行颁发权限: $su -(注意有- ,这和su是不同的,在用命令"su"的时候只是切换到root ...