skip to Main Content

I want to embed a sub module directory in parent module, but golang remind me that pattern tpl/api_new/*: cannot embed file tpl/api_new/README.md: in different module

I know that I can delete go.mod & go.sum and then run "go mod init && go get -u" when the new project is generated.

The bottom is the file tree and the embed variable, is there anything others I can do to embed go.mod & go.sum?
Thanks~

//go:embed tpl/api_new/*
var apiNew embed.FS
├─api_new
│  │  .editorconfig
│  │  .gitignore
│  │  generate.go
│  │  go.mod
│  │  go.sum
│  │  makefile
│  │  README.md
│  │
│  ├─cmd
│  │  └─app
│  │          main.go
│  │
│  ├─config
│  │      config-dev.toml
│  │      config-live.toml
│  │      config-local.toml
│  │      config-prod.toml
│  │      config-stress.toml
│  │      config-trunk.toml
│  │
│  └─internal
│      └─app
│          ├─http
│          │  │  server.go
│          │  │
│          │  └─example
│          │          hello.go
│          │
│          ├─lib
│          │  ├─err
│          │  │      codecommon.go
│          │  │      err.go
│          │  │
│          │  ├─pms
│          │  │      init.go
│          │  │
│          │  └─util
│          │          md5.go
│          │          url.go
│          │
│          ├─model
│          │  │  init.go
│          │  │
│          │  ├─grpc
│          │  │  ├─roomaggregation
│          │  │  │      aggregation.proto
│          │  │  │      base.go
│          │  │  │
│          │  │  ├─roombase
│          │  │  │      base.proto
│          │  │  │      roombase.go
│          │  │  │
│          │  │  └─roomlist
│          │  │          base.proto
│          │  │          icon.go
│          │  │
│          │  ├─hrpc
│          │  │  │  init.go
│          │  │  │
│          │  │  └─efs
│          │  │          efs.go
│          │  │          init.go
│          │  │          option.go
│          │  │
│          │  └─redis
│          │      ├─attachInfo
│          │      │      index.go
│          │      │
│          │      ├─outing
│          │      │      index.go
│          │      │
│          │      ├─roomcity
│          │      │      roomcity.go
│          │      │
│          │      └─roomjump
│          │              index.go
│          │
│          └─service
│              │  init.go
│              │
│              └─example
│                      hello.go

2

Answers


  1. is there anything others I can do to embed go.mod & go.sum?

    No.

    Login or Signup to reply.
  2. Each module within a repository is stored separately within the module cache. By design, the presence of a go.mod file in a subdirectory causes that entire subtree to be completely pruned out of the outer module.

    If you really need the individual files in tpl/api_new to be accessible to the module in the parent directory, then you can either:

    1. remove the inner go.mod and go.sum files to put the source files all in the same module, or
    2. export the embed.FS data from some package within the …/tpl/api_new module (perhaps an internal package), and import that package in the parent-directory module in order to access that data programmatically.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search