how_run

package module
v0.0.0-...-56163ad Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2023 License: MIT Imports: 5 Imported by: 1

README

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

}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RunIDE

type RunIDE int

RunIDE 是从什么IDE中运行的

const (

	// RunIDEUnknown 咱也不知道是啥IDE
	RunIDEUnknown RunIDE = iota

	// RunIDEGoland 是从GoLand中运行的
	RunIDEGoland
)

func GetRunIDE

func GetRunIDE() (RunIDE, error)

func (RunIDE) String

func (x RunIDE) String() string

type RunType

type RunType int
const (

	// RunTypeUnknown 咱也不知道咋运行的
	RunTypeUnknown RunType = iota

	// RunTypeSourceCode 是从源代码中运行的
	RunTypeSourceCode

	// RunTypeReleaseBinary 发布的二进制文件运行
	RunTypeReleaseBinary
)

func GetRunType

func GetRunType() (RunType, error)

GetRunType 获取当前程序是以什么方式运行的

func (RunType) String

func (x RunType) String() string

type SourceCodeRunType

type SourceCodeRunType int

SourceCodeRunType 如果是从源代码运行的话,则入口是啥

const (

	// SourceCodeRunTypeUnknown 只知道是从源代码中运行的,但是入口俺也不知道
	SourceCodeRunTypeUnknown SourceCodeRunType = iota

	// SourceCodeRunTypeExample 是从Example运行的
	SourceCodeRunTypeExample

	// SourceCodeRunTypeBenchmark 是从Benchmark运行的
	SourceCodeRunTypeBenchmark

	// SourceCodeRunTypeMain 是从main.go运行的
	SourceCodeRunTypeMain

	// SourceCodeRunTypeTest 是从测试用例运行的
	SourceCodeRunTypeTest
)

func GetSourceCodeRunType

func GetSourceCodeRunType() (SourceCodeRunType, error)

GetSourceCodeRunType 如果是从源代码运行的,则是以什么方式运行的

Example
runType, err := GetSourceCodeRunType()
if err != nil {
	fmt.Println(err.Error())
	return
}
fmt.Println(runType)
Output:

1

func (SourceCodeRunType) String

func (x SourceCodeRunType) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL