如何判断我是否在本地运行Google App Engine/Go?

huangapple go评论85阅读模式
英文:

How to tell whether I'm running locally on google app engine/go

问题

在Go语言中,可以使用os.Getenv("ENV_VARIABLE_NAME")函数来获取环境变量的值。要检查是否在本地运行,可以检查os.Getenv("SERVER_SOFTWARE")的值是否为空。如果为空,则表示在本地运行。以下是示例代码:

package main

import (
	"fmt"
	"os"
)

func main() {
	if os.Getenv("SERVER_SOFTWARE") == "" {
		fmt.Println("Running locally")
	} else {
		fmt.Println("Running on a server")
	}
}

请注意,SERVER_SOFTWARE环境变量在本地运行时为空,而在服务器上运行时将包含服务器软件的名称。

英文:

I'm aware of if 'APPENGINE_RUNTIME' in os.environ.keys(): for the python runtime, but is there a similar way to tell whether I'm running locally using go?

答案1

得分: 3

调用IsDevAppServer函数来确定应用程序是否在本地运行。

英文:

Call IsDevAppServer to determine if the application is running locally.

huangapple
  • 本文由 发表于 2017年3月20日 07:11:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/42893478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定