Gumbo: testable Golang code example

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

Brief introduction

The Dodoc example is an aggregation of some executable test code that they provide to the reader for reading and execution as part of the package's documentation. The reader can click the "Run" button to test the code.

Golang's standard package includes many of these code examples (such as the strings package)

This article describes how to write a similar code example.

Sample is Unit test

The code example compiles and executes as part of the package.

In a typical unit test, the example is some of the methods in the _test.go file in the package. The code sample differs from the test code in that the example method begins with Example (different from test of the code tested) and has no parameters.

stringutil 包是 golang 代码示例仓库中的一部分。下面的代码展示了他是如何演示 Reverse 的用法。
package stringutil_testimport ("fmt""github.com/golang/example/stringutil")func ExampleReverse() {fmt.Println(stringutil.Reverse("hello"))// Output: olleh}

The above code can be found in the Example_test.go file in the Stringutil directory.

Godoc This code example in the documentation for the Reverse method, as detailed in the figure:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.