美文网首页
go 检查无法运行到的代码块

go 检查无法运行到的代码块

作者: 币来币往 | 来源:发表于2018-08-09 19:54 被阅读0次

    go tool vet file.go

    go document

    我们通常所看的文档一般由两部分组成,package是干什么的,怎么用。
    干什么:我们通常会在package名,函数名前面通过注释的形式来说明这个包,函数的功能是什么。
    怎么用:通常是一个实例代码, go 语言的一个强大之处就在于,它可以很方便的将我们的测试代码中的Example,放到说明文档中去。

    下面我们通过一个例子来说明

    code

    // This package is for showcasing the documentation capabilities of Go 
    // It is a naive package! 
    package documentMe
    
    // Pie is a global variable 
    // This is a silly comment! 
    const Pie = 3.1415912
    
    // The S1() function finds the length of a string 
    // It iterates over the string using range 
    func S1(s string) int {
        if s == "" {
            return 0
        }
        n := 0
        for range s {
            n++
        }
        return n
    }
    
    // The F1() function returns the double value of its input integer 
    // A better function name would have been Double()! 
    func F1(n int) int {
        return 2 * n
    } 
    

    test

    package documentMe
    
    import (
        "fmt"
    )
    func ExampleS1() {
        fmt.Println(S1("123456789"))
        fmt.Println(S1(""))
        // Output:
        // 9
        // 0
    }
    
    func ExampleF1() {
        fmt.Println(F1(10))
        fmt.Println(F1(2))
        // Output:
        // 1
        // 55
    }
    
    安装package
    $ mkdir ~/go/src/documentMe
    $ cp documentMe.go documentMe_test.go ~/go/src/aPackage/
    $ go install documentMe
    
    通过godoc查看package 文档
    $ godoc -http=":8080"
    

    通过浏览器查看文档

    image.png

    相关文章

      网友评论

          本文标题:go 检查无法运行到的代码块

          本文链接:https://www.haomeiwen.com/subject/xvbcbftx.html