使用 Go Web App 在 Docker 中执行用户进程时出错
问题
运行 Dockerized Go Web 应用程序时,遇到错误,指示“ standard_init_linux.go:190 中没有这样的文件或目录。
Go代码
package main import ( "fmt" "net/http" "os" ) func hostHandler(w http.ResponseWriter, r *http.Request) { name, err := os.Hostname() if err != nil { panic(err) } fmt.Fprintf(w, "<h1 id="HOSTNAME-s">HOSTNAME: %s</h1><br>", name) fmt.Fprintf(w, "<h1 id="ENVIRONMENT-VARS">ENVIRONMENT VARS: </h1><br>") fmt.Fprintf(w, "
- ")
for _, evar := range os.Environ() {
fmt.Fprintf(w, "
- %s ", evar) } fmt.Fprintf(w, "
Awesome site in Go!
") fmt.Fprintf(w, "Host info
") } func main() { http.HandleFunc("/", rootHandler) http.HandleFunc("/host/", hostHandler) http.ListenAndServe(":8080", nil) }
Docker File
FROM scratch WORKDIR /home/ubuntu/go COPY webapp / EXPOSE 8080 CMD ["/webapp"]
原因及解决方案
出现错误是因为编译的Go二进制文件缺少对libc的依赖,libc是由导入net时默认。要纠正此问题,请使用以下标志编译 Go 二进制文件:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -ldflags '-w' -o mybin *.go
以上是为什么我的 Dockerized Go Web 应用程序失败并出现'没有这样的文件或目录”错误?的详细内容。更多信息请关注PHP中文网其他相关文章!

goisastrongchoiceforprojectsneedingsimplicity,绩效和引发性,butitmaylackinadvancedfeatures and ecosystemmaturity.1)

Go'sinitfunctionandJava'sstaticinitializersbothservetosetupenvironmentsbeforethemainfunction,buttheydifferinexecutionandcontrol.Go'sinitissimpleandautomatic,suitableforbasicsetupsbutcanleadtocomplexityifoverused.Java'sstaticinitializersoffermorecontr

thecommonusecasesfortheinitfunctionoare:1)加载configurationfilesbeforeThemainProgramStarts,2)初始化的globalvariables和3)runningpre-checkSorvalidationsbeforEtheprofforeTheProgrecce.TheInitFunctionIsautefunctionIsautomentycalomationalmatomatimationalycalmatemationalcalledbebeforethemainfuniinfuninfuntuntion

ChannelsarecrucialingoforenablingsafeandefficityCommunicationBetnewengoroutines.theyfacilitateSynChronizationAndManageGoroutIneLifeCycle,EssentialforConcurrentProgramming.ChannelSallSallSallSallSallowSallowsAllowsEnderDendingAndReceivingValues,ActassignalsignalsforsynChronization,and actassignalsynChronization and andsupppor

在Go中,可以通过errors.Wrap和errors.Unwrap方法来包装错误并添加上下文。1)使用errors包的新功能,可以在错误传播过程中添加上下文信息。2)通过fmt.Errorf和%w包装错误,帮助定位问题。3)自定义错误类型可以创建更具语义化的错误,增强错误处理的表达能力。

Gooffersrobustfeaturesforsecurecoding,butdevelopersmustimplementsecuritybestpracticeseffectively.1)UseGo'scryptopackageforsecuredatahandling.2)Manageconcurrencywithsynchronizationprimitivestopreventraceconditions.3)SanitizeexternalinputstoavoidSQLinj

Go的错误接口定义为typeerrorinterface{Error()string},允许任何实现Error()方法的类型被视为错误。使用步骤如下:1.基本检查和记录错误,例如iferr!=nil{log.Printf("Anerroroccurred:%v",err)return}。2.创建自定义错误类型以提供更多信息,如typeMyErrorstruct{MsgstringDetailstring}。3.使用错误包装(自Go1.13起)来添加上下文而不丢失原始错误信息,

对效率的Handleerrorsinconcurrentgopragrs,UsechannelstocommunicateErrors,EmparterRorwatchers,InsterTimeouts,UsebufferedChannels和Provideclearrormessages.1)USEchannelelStopassErstopassErrorsErtopassErrorsErrorsFromGoroutInestotheStothemainfunction.2)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 Linux新版
SublimeText3 Linux最新版