First glance in Go
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的更多相关文章
- 理解 Glance - 每天5分钟玩转 OpenStack(20)
OpenStack 由 Glance 提供 Image 服务. 理解 Image 要理解 Image Service 先得搞清楚什么是 Image 以及为什么要用 Image? 在传统 IT 环境下, ...
- 解决在CentOS6.5下安装OpenStack(Icehouse版本 )出现的glance服务无法正常工作的问题
最近一直在用Juno版本,因为项目需要,今天在虚拟机里安装了Icehouse版,其中glance组件在执行安装的过程后,出现启动失败的现象,幸好以前排查过此类错误,遂记录如下: 在官方文档(Iceho ...
- 4.openstack之mitaka搭建glance镜像服务
部署镜像服务 一:安装和配置服务 1.建库建用户 mysql -u root -p CREATE DATABASE glance; GRANT ALL PRIVILEGES ON glance.* T ...
- Ceph与OpenStack的Glance相结合
http://docs.ceph.com/docs/master/rbd/rbd-openstack/?highlight=nova#kilo 在Ceoh的admin-node上进行如下操作: 1. ...
- 探索 OpenStack 之(10):深入镜像服务Glance
本篇博文来探讨下镜像服务Glance. 0. 基本概念 0.1 基本功能 Glance提供REST API来支持以下镜像操作: 查询 注册 上传 获取 删除 访问权限管理 0.2 Glance RE ...
- 【OpenStack】OpenStack系列4之Glance详解
下载安装 参考:http://www.linuxidc.com/Linux/2012-08/68964.htm http://www.it165.net/os/html/201402/7246.htm ...
- 这个Glance的界面该怎么看出问题,为什么状态是SOCKT?
这个glance的状态图有问题吗?
- Centos7 install Openstack - (第三节)添加镜像服务(Glance)
Centos7 install Openstack - (第三节)添加镜像服务(Glance) 我的blog地址:http://www.cnblogs.com/caoguo 该文根据openstack ...
- OpenStack:安装Glance
>安装Glance1. 安装# apt-get install glance python-glanceclient删除sqlite文件rm -f /var/lib/glance/glance. ...
- 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 ...
随机推荐
- 2016弱校联盟十一专场10.5---As Easy As Possible(倍增)
题目链接 https://acm.bnu.edu.cn/v3/contest_show.php?cid=8506#problem/A problem description As we know, t ...
- SpringMVC框架介绍
1. SpringMVC通过一套MVC注解,让POJO成为处理请求的控制器,而无须实现任何接口. 2.支持REST风格的URL请求. 3.采用了松散耦合可插拔组件结构,比其他MVC框架更具扩展性和灵 ...
- php中opendir函数用法实例
这篇文章主要介绍了php中opendir函数用法,以实例形式详细讲述了opendir函数打开目录的用法及相关的注意事项,具有一定的参考借鉴价值,需要的朋友可以参考下 本文实例分析了php中opendi ...
- 跨平台日志清理工具 Log-Cutter v2.0.1 RC-1 发布
Log-Cutter 是JessMA开源组织开发的一个简单实用的日志切割清理工具.对于服务器的日常维护来说,日志清理是非常重要的事情,如果残留日志过多则严重浪费磁盘空间同时影响服务的性能.如果用手工方 ...
- CMS
一.任务简介: 开发简单的CMS.在数据库中创建新闻数据库表news,包含(题目.作者.日期.正文等字段):创建HTML模板文件:读取数据库所有数据的信息,并使用新闻信息 替换模板文件中的占位符,从而 ...
- Exif.js 读取图像的元数据
Exif.js 提供了 JavaScript 读取图像的原始数据的功能扩展,例如:拍照方向.相机设备型号.拍摄时间.ISO 感光度.GPS 地理位置等数据. 注意事项: EXIF 数据主要来自拍摄的照 ...
- 【初窥javascript奥秘之事件机制】论“点透”与“鬼点击”
前言 最近好好的研究了一番移动设备的点击响应速度,期间不断的被自己坑,最后搞得焦头烂额,就是现在可能还有一些问题,但是过程中感觉自己成长不少, 最后居然感觉对javascript事件机制有了更好的认识 ...
- pdo mysql错误:Cannot execute queries while other unbuffered queries are active
运行环境:PHP 5.5.30-x64,MYSQL 5.6.27 错误代码:Cannot execute queries while other unbuffered queries are act ...
- [实现]Javascript代码的另一种压缩与加密方法——代码图片转换
代码=图片 图片=代码 JS代码对于喜欢F12的同志来说,连个遮羞布都没有... 虽然把代码变成图片也仅仅只是增加一层纱布而已...但这方法还是挺好玩的,而且代码也被压缩了一点. 第一次看到[图片=代 ...
- css伪元素实现tootip提示框
先看效果 废话不说,直接上图(请把鼠标移到我的头像上),看看今天要做的是什么: 实现原理 这些提示框原理都是一样的,且只需一个div标签就能实现,当然也有笨方法,比如用多个标签相互重叠.遮盖,或者干脆 ...