dqxyh48864 2019-01-17 18:45
浏览 10
已采纳

使用相同的程序包会污染已编译的二进制文件吗?

TL;DR:

Do test written within a package end up in the final exported package? Do they add any garbage or weight to a compiled binary?

Longer version:

Let's say that I have a foo Go package:

pkg/
  foo/
    bar.go
    bar_test.go

I'm aware of the black box vs white box approaches to testing in go. A short recap is that I can either:

  1. have bar_test.go declare a foo_test package, or
  2. have it part of the main foo package.

Approach 1 provides better isolation because it allows to focus on the public API of the package, as one only access the exported identifiers of foo. Also, when application code imports the foo package with import "pkg/foo", only the files containing the main foo package are compiled. That's nice. [1]

However, there are cases where putting the tests in foo is a convenient compromise. I don't particularly like it myself, but I can see it in several codebases and I understand why at times it's necessary.

My question is about what happens to these tests. Since they're part of the package foo, when foo is imported somewhere, I'd expect the tests to be brought along. Or is the compiler smart enough to strip them?


[1] this is not a question on what approach is "better". It's doesn't really matter. Please abstain from commenting with "that approach is not better".

  • 写回答

1条回答 默认 最新

  • douqi1928 2019-01-17 18:54
    关注

    Yes, the Go tool is smart enough not to pass them to the compiler.

    https://github.com/golang/go/issues/10184

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?