Go的内部软件包 [英] Internal packages in Go

查看:240
本文介绍了Go的内部软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Go中导入内部包?

  import(

runtime / internal /原子
runtime / internal / sys

得到一个错误:

lockquote

import runtime / internal / atomic:不允许使用内部包


并在主包中使用内部函数?

解决方案

背景



Go鼓励将程序构建为使用导出的API进行交互的包集合。但是,所有包都可以导入。这在实现一个库或命令时会产生一种紧张感:它可能会增长到足以构建多个包,但拆分它会将这些附加包中使用的API导出到全世界。能够创建具有限制可见性的包可以消除这种紧张局势。



一个规则提议Go 1.4



如果导入代码位于以internal目录的父级为根的树之外,则不允许导入包含internal元素的路径。



简短回答< h3>

你不能(至少很容易)也不应该这样做。


How to import internals packages in Go ?

import (

    "runtime/internal/atomic"
    "runtime/internal/sys"
)

Like this without get a error:

imports runtime/internal/atomic: use of internal package not allowed

And use internal funcs in a main package ?

解决方案

Background

Go encourages structuring a program as a collection of packages interacting using exported APIs. However, all packages can be imported. This creates a tension when implementing a library or command: it may grow large enough to structure as multiple packages, but splitting it would export the API used in those additional packages to the world. Being able to create packages with restricted visibility would eliminate this tension.

A rule proposed to Go 1.4

An import of a path containing the element "internal" is disallowed if the importing code is outside the tree rooted at the parent of the "internal" directory.

Short answer

You can't (at least easily) and you shouldn't.

这篇关于Go的内部软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆