Go Language learning Tips (ii)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Golang compile-time set version number

No matter what language you write the code in, the version is a very important thing. In general, it is a good idea to use the-V or--version parameters to output the version of the program. Words do not say much, on the code snippet.

package mainimport (    "flag"    "fmt"    "os")var VersionStr = "unknown"func main() {    version := flag.Bool("v", false, "Show Version")    flag.Parse()    if *version {        fmt.Println(VersionStr)        os.Exit(0)    }}

Compile script:

#!/bin/bash -xefunction getversion() {    branch=`git rev-parse --abbrev-ref HEAD`    commitid=`git rev-parse --short HEAD`    builddate=`date +%Y%m%d-%H%M%S`    echo $branch-$commitid-$builddate}cd `dirname $0`# use go vendorexport GO15VENDOREXPERIMENT=1version=`getversion`pkgpath="github.com/auxten/logrus/examples/basic"echo "build log-linux"GOOS=linux GOARCH=amd64  go build -ldflags "-X main.VersionStr=${version}"  -o bin/log-linux ${pkgpath}#echo "build log-osx"#GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.VersionStr=${version}"  -o bin/log-osx ${pkgpath}#echo "build log-win#GOOS=windows GOARCH=386  go build -ldflags "-X main.versionStr=${version}"  -o bin/log-win ${pkgpath}echo "done"

This way, you can put the GIT branch-commitid-compile time output by running./bin/log-linux-v.

Copyright NOTICE: Reprint Please indicate article source: Reboot-51

    • If the article is helpful to you, please pay attention to, click a praise!
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.