Skip to content

检测Golang程序是如何运行的,是从打包的二进制运行的,还是从源代码运行的,如果是源代码又是Test还是main啥的

License

Notifications You must be signed in to change notification settings

golang-infrastructure/go-how-run

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How Run

一、这是啥

用来判断当前程序是怎么跑起来的,比如是源代码运行的,还是编译后运行的,是运行的单元测试,还是main方法运行的,是否跑在IDE比如GoLand中

二、安装

go get -u github.com/golang-infrastructure/go-how-run

三、Example

package main

import (
	"fmt"
	how_run "github.com/golang-infrastructure/go-how-run"
)

func main() {

	// 识别当前是运行的发布的二进制包还是从源代码运行
	runType, err := how_run.GetRunType()
	if err != nil {
		fmt.Println("GetRunType error: " + err.Error())
		return
	}
	fmt.Println(runType) // Output: SourceCode

	// 如果是从源代码运行,入口是啥,是main方法还是单元测试啥的
	sourceCodeRunType, err := how_run.GetSourceCodeRunType()
	if err != nil {
		fmt.Println("GetSourceCodeRunType error: " + err.Error())
		return
	}
	fmt.Println(sourceCodeRunType) // Output: main.go

	// 识别当前是否运行在IDE中
	ide, err := how_run.GetRunIDE()
	if err != nil {
		fmt.Println("GetRunIDE error: " + err.Error())
		return
	}
	fmt.Println(ide) // Output: GoLand

}

About

检测Golang程序是如何运行的,是从打包的二进制运行的,还是从源代码运行的,如果是源代码又是Test还是main啥的

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages