分类 默认分类 下的文章

golang 1.16 版提供了资源内嵌

import _ "embed"

//go:embed hello.txt
var s string
print(s)


import _ "embed"

//go:embed hello.txt
var b []byte
print(string(b))


import "embed"

//go:embed hello.txt
var f embed.FS
data, _ := f.ReadFile("hello.txt")
print(string(data))


package server

import "embed"

// content holds our static web server content.
//go:embed image/* template/*
//go:embed html/index.html
var content embed.FS

func main() {
    http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(content))))
    http.ListenAndServe(":8080", nil)
}

参考资料:
https://golang.org/pkg/embed/

使用 docker 的 multistage 就可以实现在docker镜像中编译golang程序,并打包可执行为镜像。

FROM golang:1.15-alpine AS builder
WORKDIR /workspace
ENV GO111MODULE=on \
    GOPROXY=https://goproxy.cn,direct

# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
COPY go.mod go.mod
COPY go.sum go.sum
RUN go mod download

# src code
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64  go build -o main ./cmd/server

FROM alpine:3.12
COPY --from=builder /workspace/main /main
RUN chmod +x /main
ENV TZ=Asia/Shanghai
ENTRYPOINT ["/main"]

参考资料:
https://segmentfault.com/a/1190000039727679

默认是从 unpkg.js 中加载的可以通过设置常量参数来修改加载路径。

flutter build web --dart-define=FLUTTER_WEB_CANVASKIT_URL=/
表示从当前根目录加载,会加载 canvaskit.js canvaskit.wasm

只有在编译模式下才会生效

const VERSION = String.fromEnvironment('VERSION', 'v0.0.0'); // 切记!!必须是常量类型才能替换
const PLATFORM = String.fromEnvironment('PLATFORM', 'android');

main() {
    print("VERSION: " + VERSION);
    print(const String.fromEnvironment('PLATFORM', 'android')); // 以常量形式访问
}

dart compile exe -DVERSION=v1.0.1 -DPLATFORM=android main.dart
dart -DVERSION=v1.0.1 -DPLATFORM=android main.dart
flutter build web --dart-define=VERSION=v1.0.1 --dart-define=PLATFORM=android