import remote project

This commit is contained in:
Robert Jeutter 2021-12-30 20:35:06 +01:00
parent 956f2b3ca5
commit d7f0e206ca
5 changed files with 64 additions and 0 deletions

5
3-importremote/go.mod Normal file
View File

@ -0,0 +1,5 @@
module example/user/importremote
go 1.17
require github.com/google/go-cmp v0.5.6

4
3-importremote/go.sum Normal file
View File

@ -0,0 +1,4 @@
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

14
3-importremote/hello.go Normal file
View File

@ -0,0 +1,14 @@
package main
import (
"fmt"
"example/user/importremote/importpackage"
"github.com/google/go-cmp/cmp"
)
func main() {
fmt.Println(importpackage.ReverseRunes("!oG ,olleH"))
fmt.Println(cmp.Diff("Hello World", "Hello Go"))
}

View File

@ -0,0 +1,12 @@
// Package importpackage implements additional functions to manipulate UTF-8
// encoded strings, beyond what is provided in the standard "strings" package.
package importpackage
// ReverseRunes returns its argument string reversed rune-wise left to right.
func ReverseRunes(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}

29
3-importremote/readme.md Normal file
View File

@ -0,0 +1,29 @@
# Import package from remote modules
An import path can describe how to obtain the package source code using a revision control system such as Git or Mercurial. The go tool uses this property to automatically fetch packages from remote repositories.
```bash
cp -r 2-importpackage 3-importremote
# change importpackage to importremote
nano go.mod
nano importpackage/reverse.go
# update modules in hello.go
nano hello.go
> package main
>
> import (
> "fmt"
>
> "example/user/importremote/importpackage"
>
> "github.com/google/go-cmp/cmp"
> )
>
> func main() {
> fmt.Println(importpackage.ReverseRunes("!oG ,olleH"))
> fmt.Println(cmp.Diff("Hello World", "Hello Go"))
> }
# get all requirements and download modules
go mod tidy
# run code
go run hello.go
```