美文网首页
「golang」Linux echo命令3种实现

「golang」Linux echo命令3种实现

作者: 王诗翔 | 来源:发表于2019-05-14 23:44 被阅读0次

    1:

    // Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
    // License: https://creativecommons.org/licenses/by-nc-sa/4.0/
    package main
    
    import (
        "fmt"
        "os"
    )
    
    func main() {
        var s, sep string
        for i := 1; i < len(os.Args); i++ {
            s += sep + os.Args[i]
            sep = " "
        }
        fmt.Println(s)
    }
    

    2:

    package main
    
    import (
        "fmt"
        "os"
    )
    
    func main() {
        s, sep := "", ""
        for _, arg := range os.Args[1:] {
            s += sep + arg
            sep = " "
        }
        fmt.Println(s)
    }
    

    3:

    package main
    
    import (
        "fmt"
        "os"
        "strings"
    )
    
    //!+
    func main() {
        fmt.Println(strings.Join(os.Args[1:], " "))
    }
    

    相关文章

      网友评论

          本文标题:「golang」Linux echo命令3种实现

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