You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Extra modules cleanup into a script and document it
**What**
- Extract the vendor and modules cleanup into a separate shell script.
This provides more space for documenting each step cleanly.
- This scripting also removes the need for the `GO_REPLACE.txt` hack,
it will automatically modify any local replacements in the original
go.mod file, so that they work as expected.
- Update the README to describe how dependencies work in the three
supported modes: modules with vendoring, modules without vendoring,
and traditional vendoring via dep.
Signed-off-by: Lucas Roesler <[email protected]>
Copy file name to clipboardExpand all lines: README.md
+35-7
Original file line number
Diff line number
Diff line change
@@ -28,6 +28,7 @@ The two templates are equivalent with `golang-http` using a structured request/r
28
28
You can manage dependencies in one of the following ways:
29
29
30
30
* To use Go modules without vendoring, add `--build-arg GO111MODULE=on` to `faas-cli up`, you can also use `--build-arg GOPROXY=https://` if you want to use your own mirror for the modules
31
+
* You can also Go modules with vendoring, run `go mod vendor` in your function folder and add `--build-arg GO111MODULE=on` to `faas-cli up`
31
32
* For traditional vendoring with `dep` give no argument, or add `--build-arg GO111MODULE=off` to `faas-cli up`
32
33
33
34
## 1.0 golang-http
@@ -361,7 +362,37 @@ func Handle(w http.ResponseWriter, r *http.Request) {
361
362
}
362
363
```
363
364
364
-
#### Advanced usage - Go sub-modules via `GO_REPLACE.txt`
365
+
#### Advanced usage
366
+
367
+
##### Sub-packages
368
+
369
+
It is often natural to organize your code into sub-packages, for example you may have a function with the following structure
370
+
371
+
```
372
+
./echo
373
+
├── go.mod
374
+
├── go.sum
375
+
├── handler.go
376
+
└── pkg
377
+
└── version
378
+
└── version.go
379
+
```
380
+
381
+
First update your go.mod file to replace `handler/function` with your local folder
382
+
383
+
```go
384
+
go mod edit -replace=handler/function=./
385
+
```
386
+
387
+
Now if you want to reference the`version` sub-package, import it as
388
+
389
+
```go
390
+
import"handler/function/pkg/version"
391
+
```
392
+
393
+
This replacement is handled gracefully by the template at build time and your local development environment will now recognize the sub-package.
394
+
395
+
##### Go sub-modules
365
396
366
397
For this example you will need to be using Go 1.13 or newer and Go modules, enable this via `faas-cli build --build-arg GO111MODULE=on`.
367
398
@@ -384,16 +415,13 @@ func Echo(w http.ResponseWriter, r *http.Request) {
384
415
}
385
416
```
386
417
387
-
To include a relative module such as this new `handlers` package, you should create a `GO_REPLACE.txt` file as follows:
418
+
To include a relative module such as this new `handlers` package, you should update your `go.mod` file as follows:
go mod edit -replace=github.com/alexellis/vault/purchase/handlers=./handlers
391
422
```
392
423
393
-
How did we get to that? Let's say your GOPATH for your GitHub repo is: `github.com/alexellis/vault/` and your OpenFaaS function is `purchase` generated by `faas-cli new purchase --lang golang-middleware`.
394
-
395
-
Your relative GOPATH is: `github.com/alexellis/vault/purchase`, so add a redirect as per below to redirect the "handlers" package to where it exists in the build container.
396
-
424
+
At build time, this relative path will be handled correctly inside the template.
397
425
398
426
Now if you want to reference the handlers package from within your `handler.go` write the following:
0 commit comments