doufang6268 2014-06-26 02:01
浏览 26
已采纳

go(golang)测试框架中“示例”的意义是什么?

I was recently reading on testing and examples in the go testing framework and did not really understand what they were for. I see that the documentation says:

The package also runs and verifies example code. Example functions may include a concluding line comment that begins with "Output:" and is compared with the standard output of the function when the tests are run. (The comparison ignores leading and trailing space.)

However, I don't really appreciate the reason that this would exist. As I write my tests, it seems that it should be more than clear how to use the code just by reading the unit tests and the benchmarks. What additional motivation does the example section provide? It seems redundant to me, however, I am sure that the inventors of go put it for a good reason, specially because they seem to empathizes good programming practice by the design of their language. I hope to understand either their motivation or how this part of language can be used in a positive way in a golang project! :)

  • 写回答

1条回答 默认 最新

  • doujiangqu2823 2014-06-26 02:15
    关注

    In Go, the source code is also used to generate documentation. Examples are not meant for testing but rather for the documentation. By having compiled examples, you can make sure they are working properly for anyone using them.

    Examples found in the standard libraries can be run directly in the browser when using golang.org to browse the documentation. Godoc.org instead links your example code directly to play.golang.org where you can try it out.

    As an example or Example usage, see: http://golang.org/pkg/sort/

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

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端