参考: https://groups.google.com/forum/#!topic/golang-nuts/oawcWAhO4Ow

Hi,

Nan Xiao <xiaona...@gmail.com> writes: 
> (1) GOPATH="/root/go"; 
> (2) There is a folder whose name is "wire" in /root/go/src; 
> (3) There is a file(a.go) in wire folder: 
> package shark

What you describe, is a package "shark", with the import path 
"wire". So:

> (4) hello.go:

Try instead:

> package main 

> import ( 
>         "wire" 
> ) 

> func main() { 
>         shark.Hello() 
> }

And now you know, why there is the strong suggestion, to have the 
package name and the last component of the import path the same. From 
this package main, there is *no way* to deduce, that shark.Hello() is 
from the package with import path "wire", except looking at the 
sourcecode.

My suggestion is: 
a) Name the folder /root/go/src/shark (import path "shark") and the package shark 
b) Name the folder /root/go/src/wire (import path "wire") and the package wire 
c) Name the folder /root/go/src/wire/shark (import path "wire/shark") and the package shark

So, you are confused by there being *three* concepts: 
a) the import path 
b) the location on disk 
c) the package name

a and b are *strongly* related (location = $GOPATH/importpath). But c is 
only related to a and b by convention.

Best,

Axel

-------------------------------------------------------------------------------------------------------------------------------------------

import 中指定的名称,其实是 “包” 所在的路径(磁盘路径), package 的名称不一定要和所在的目录名称一样。

main.go

package main

import "fmt"
import (
// "../demo/f1"
"./f1" //import这里的,其实是磁盘路径名。
) func say() {
fmt.Println("say function call!")
} func main() {
fmt.Println("hello, world") say()
// fly()
p_f1.F1() //这里的和文件里面的 package 定义的报名一致就可以了。不一定要和目录名f1 同名, 可以随便命名,如这里的p_f1.
//但是同一个目录下,不用的文件中的 package 定义的包名 也只能有一个!!
// f1.F2()
}

  f1.go

package p_f1

import (
"fmt"
) func F1() {
fmt.Println("package demo f1 func call")
}

  f2.go

package p_f1

import (
"fmt"
) func F2() {
fmt.Println("package f1 f2 func call.")
}

  go run mian.go

稍微改一下,如果把f2.go 的第一行 package p_f1 改为 f1 , 运行: go run main.go, 报错:

go 中的pacage 名称 和import {}中的名称的更多相关文章

  1. Hibernate 3中如何获得库表所有字段的名称

    15问:Hibernate 3中如何获得库表所有字段的名称 答:可以使用以下的程序获得. Configuration conf = new Configuration(); conf.configur ...

  2. SQL-28 查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部

    题目描述 film表 字段 说明 film_id 电影id title 电影名称 description 电影描述信息 CREATE TABLE IF NOT EXISTS film ( film_i ...

  3. SpringMVC请求使用@PathVariable获取文件名称并且文件名中存在.导致路径被截取的问题

    在SpringMVC中,当使用@pathVariable通过Get请求获取路径名称时,如果路径名称上存在小数点,则获取不到小数点后面的内容,会被Spring截取. 比如我获取某一文件,路径是local ...

  4. eclipse批量修改package、import中的包名

    问题:想把以前开发的包,用到新项目中,怎么操作呢? 解决方案: 把文件夹复制到新项目包中,同时需要更改很多package.import中的包名第一步:打开一个java文件,选中要替换的字段: 第二步: ...

  5. python接口自动化测试二十七:密码MD5加密 ''' MD5加密 ''' # 由于MD5模块在python3中被移除 # 在python3中使用hashlib模块进行md5操作 import hashlib # 待加密信息 str = 'asdas89799,.//plrmf' # 创建md5对象 hl = hashlib.md5() # Tips # 此处必须声明encode # 若写法为

    python接口自动化测试二十七:密码MD5加密   ''' MD5加密 '''# 由于MD5模块在python3中被移除# 在python3中使用hashlib模块进行md5操作import has ...

  6. Node中导入模块require和import??

    转自:https://blog.csdn.net/wxl1555/article/details/80852326 S6标准发布后,module成为标准,标准的使用是以export指令导出接口,以im ...

  7. Windows修改账户名称和任务管理器中服务对应的用户名称

    新安装的Windows10,在激活的时候如果选择使用微软账户登录,比如我的微软账户名是QQ邮箱,系统激活后,系统盘用户目录(users)下面的用户目录文件夹名称为QQ邮箱的前几位数字,这样其实也在使用 ...

  8. Android应用项目中BaseAdapter、SimpleAdapter和ArrayAdapter中的三种适配器

    一.写在前面: 本次我们来讲解一下Android应用中三个适配器:BaseAdapter.SimpleAdapter和ArrayAdapter.其中常见的是BaseAdapter,也是个人推荐使用的适 ...

  9. 多线程查询数据,将结果存入到redis中,最后批量从redis中取数据批量插入数据库中【我】

    多线程查询数据,将结果存入到redis中,最后批量从redis中取数据批量插入数据库中 package com.xxx.xx.reve.service; import java.util.ArrayL ...

随机推荐

  1. kernel_read【转】

    转自:http://blog.csdn.net/echoisland/article/details/7101097http://lxr.oss.org.cn/source/fs/exec.c 798 ...

  2. 【Android开发日记】之入门篇(十四)——Button控件+自定义Button控件

        好久不见,又是一个新的学期开始了,为什么我感觉好惆怅啊!这一周也发生了不少事情,节假日放了三天的假(好久没有这么悠闲过了),实习公司那边被组长半强制性的要求去解决一个后台登陆的问题,结果就是把 ...

  3. .tar与.gz有什么联系与区别?

    我们在windows下经常进行压缩解压等操作.在linux上也经常会用到,但是比较常见的是.tar.gz 很多人见到就条件反射,压缩用czvf,解压用xzvf.但是,你真的了解这个后缀名吗? 其实: ...

  4. python的上下文管理(contextlib)(2)

    contextlib是一个Python模块,作用是提供更易用的上下文管理器. 编写 __enter__ 和 __exit__ 仍然很繁琐,因此Python的标准库 contextlib 提供了更简单的 ...

  5. Arduino mega 2560驱动安装失败(没有建立对验证码(TM)签名的目录的发布者信任)的解决方法

    转载请注明出处,谢谢...... 放假的时候在自己家台式机上安装时候是很顺畅的,今天在自己本子上安装的时候就不行了~ IDE版本:1.05 问题描述:在网上搜索了相关问题,发现绝大部分安装失败的时候都 ...

  6. tomcat的环境配置

    针对java8 选择tomcat进行配置 Tomcat 环境变量配置: 下载:http://tomcat.apache.org/download-80.cgi 解压后直接copy到C盘,这是免安装版本 ...

  7. FZU 2150 Fire Game 【两点BFS】

    Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns) ...

  8. 训练指南 UVA - 11354(最小生成树 + 倍增LCA)

    layout: post title: 训练指南 UVA - 11354(最小生成树 + 倍增LCA) author: "luowentaoaa" catalog: true ma ...

  9. csu1216( Trie )

    csu1216 题意 给定一些数,求这些数中两个数的异或值最大的那个值. 分析 转化成二进制数存入字典树,比如说要查询 \(0011\) ,显然和 \(1100\) 结合最优,所以我们直接在字典树上寻 ...

  10. KMP+差分 文章过滤器 (filter)

    Description 给定一些短串,要求你在一个长串中,将这些短串部分变为\(*\) Input 第一行包括一个整数\(n\),表示短串的数量. 接下来的\(n\)行,为\(n\)个短串. 最后一行 ...