zoukankan      html  css  js  c++  java
  • 如何快速生成一个golang的demo工程进行测试?

    脚本如下

    #!/usr/bin/env bash
    [[ -n $DEBUG ]] && set -x
    set -eou pipefail
    
    useage(){
      cat <<HELP
    USAGE:
        new
    HELP
    }
    
    exit_err() {
       echo >&2 "${1}"
       exit 1
    }
    
    if [ $# -lt 0 ];then
        useage
        exit 1
    fi
    
    DEMODIR=$(mktemp -d)
    echo "${DEMODIR}"
    cd "${DEMODIR}"
    go mod init demo
    cat > "${DEMODIR}"/Makefile <<EOF
    all: lint test run
    .PHONY: lint test run
    run:
    	go run main.go
    build:
    	go build
    lint:
    	golangci-lint run
    test:
    	go test demo -run Testdemo
    bench:
    	go test -benchmem -bench Benchmark_demo
    EOF
    cat > "${DEMODIR}"/main.go <<EOF
    package main
    import (
    	"fmt"
    )
    
    func demo() string {
    	return "hello world"
    }
    
    func main(){
      fmt.Println(demo())
    }
    EOF
    cat > "${DEMODIR}"/main_test.go <<EOF
    package main
    
    import (
    	"testing"
    )
    
    func Test_demo(t *testing.T){
      result := demo()
      if result != "hello world" {
        t.Errorf("want %v, get %v", "hello world", result)
      }
    }
    
    func Benchmark_demo(b *testing.B){
      result := demo()
      if result != "hello world" {
        b.Errorf("want %v, get %v", "hello world", result)
      }
    }
    EOF
    code .
    
    

    使用方法

    godemo
    
  • 相关阅读:
    假丶依赖注入
    .NET Core 傻瓜式CSRedisCore缓存
    .NET Core MD5加密 32位和16位
    数据库所对应的函数
    使用通配符进行过滤
    WHERE 子句操作符
    不同的数据库查询行数的方式
    第一课了解SQL
    微服务架构综述
    三层应用与单块架构
  • 原文地址:https://www.cnblogs.com/futuretea/p/11997769.html
Copyright © 2011-2022 走看看