解决go build 构建问题 go:build comment without // +build comment

文章描述了一个使用gobuild构建Go程序时遇到的问题,该问题由于Go版本低于1.17导致,新引入的构建标记语法引起不兼容。作者升级Go版本至1.17.13后,问题得到解决,能够成功构建并运行程序,监听8081端口并打印访问日志。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录一个go build 构建的问题

main.go 文件

package main

import (
	"github.com/valyala/fasthttp"
	"go.uber.org/zap"
)

var logger *zap.Logger

func init() {
	logger, _ = zap.NewProduction()
}

func fastHTTPHandler(ctx *fasthttp.RequestCtx) {
	logger.Info("hell, go module", zap.ByteString("uri", ctx.RequestURI()))
}

func main() {
	fasthttp.ListenAndServe(":8081", fastHTTPHandler)
}

脚本功能是监听8081端口访问,打印访问的头信息日志

执行go build 构建 报错

main.go:5:2: //go:build comment without // +build comment

排查了代码应该是没有问题,必应网站查询问题原因,可能是go 的版本 是低于1.17版本 导致出现的问题,go 引入的依赖库包中 1.17版本后 引入了新的构建标记语法导致的兼容问题

查看当前使用的go版本

##的确是低于 1.17 版本
PS D:\coder\gotestprojects\hellomodule> go version
go version go1.16.5 windows/amd64
###安装go1.17.13 版本
PS D:\coder\gotestprojects\hellomodule> go version
go version go1.17.13 windows/amd64
### 重新执行go build 命令执行成功

查询解决问题参考链接
https://github.com/bitfield/script/issues/131
https://it-explain.com/go-golang-build-error-gobuild-comment-without-build-comment/

### C++ Release Mode Runtime Library Configuration and Usage In the context of configuring a project to use specific runtime libraries during compilation, especially under release configurations, it is important to understand how these settings influence performance and compatibility. For projects using compilers like those from Microsoft Visual Studio (MSVC), setting up the correct runtime library can be done through compiler flags or within build configuration files. For instance, when specifying that native compiler configurations should include options for building in release mode, one might add such lines as `_native_compiler_configs += ["//build/config:release"]`[^1]. This line indicates an addition to the build system's instructions ensuring that certain optimizations typical of release builds are enabled. When focusing on MSVC specifically, choosing between static (`/MT`) or dynamic (`/MD`) linking with the CRT (C Run-Time) library plays a crucial role: - **Static Linking (/MT)**: Links directly against precompiled binaries provided by the toolchain without requiring external DLLs at runtime. - **Dynamic Linking (/MD)**: Uses shared versions of the standard C/C++ libraries which must exist alongside executables but allows multiple applications to share single copies reducing overall memory footprint. To configure this choice programmatically via command-line arguments passed into cl.exe—the front end for the MSVC Compiler—one would append either `/MT` or `/MD`, depending upon preference towards statically linked versus dynamically loaded runtimes respectively. In practice, many developers opt for `/MD` because it facilitates easier deployment across different environments while keeping binary sizes manageable. Here’s an example demonstrating how to set these parameters explicitly within code comments intended only for illustrative purposes since actual implementation depends heavily on integrated development environment setup scripts or makefiles used: ```cpp #pragma comment(lib,"msvcrt") // Indicates intention to link against multithreaded debug version; adjust accordingly based on desired target e.g., "libcmt" for non-debug static linkage int main() { // Application entry point here... } ``` Additionally, creating a proper debugging environment involves establishing appropriate conditions where necessary diagnostics tools function correctly—a process often facilitated by including directives similar to what was mentioned earlier about adding `["//build/config:debug"]` entries[^2]. --related questions-- 1. What differences exist between Multi-threaded Debug DLL (/MDd) and its Release counterpart? 2. How does selecting Static vs Dynamic linking impact application startup times? 3. Can mixing different types of runtime libraries lead to issues? If so, provide examples. 4. Are there any best practices regarding managing dependencies when deploying software built with dynamic linking? 5. Under what circumstances might someone prefer static over dynamic linking despite increased executable size?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值