import package project
This commit is contained in:
parent
abe8ee6c91
commit
956f2b3ca5
3
2-importpackage/go.mod
Normal file
3
2-importpackage/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module example/user/importpackage
|
||||
|
||||
go 1.17
|
11
2-importpackage/hello.go
Normal file
11
2-importpackage/hello.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"example/user/importpackage/importpackage"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(importpackage.ReverseRunes("!oG ,olleH"))
|
||||
}
|
12
2-importpackage/importpackage/reverse.go
Normal file
12
2-importpackage/importpackage/reverse.go
Normal 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)
|
||||
}
|
50
2-importpackage/readme.md
Normal file
50
2-importpackage/readme.md
Normal file
@ -0,0 +1,50 @@
|
||||
# Import package
|
||||
write a `importpackage` package and use it from the hello program.
|
||||
|
||||
Because the `ReverseRunes` function begins with an upper-case letter, it is exported and can be used in other packages that import the importpackage package.
|
||||
|
||||
Test that the package compiles with go build:
|
||||
```bash
|
||||
# init project
|
||||
mkdir 2-importpackage
|
||||
cd 2-importpackage
|
||||
go mod init example/user/importpackage
|
||||
# create files
|
||||
touch hello.go
|
||||
mkdir importpackage
|
||||
cd importpackage
|
||||
nano reverse.go
|
||||
> // 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)
|
||||
> }
|
||||
go build
|
||||
```
|
||||
|
||||
This won't produce an output file. Instead it saves the compiled package in the local build cache.
|
||||
|
||||
After confirming that the importpackage package builds, use it from the `hello` program.
|
||||
```bash
|
||||
cd ..
|
||||
nano hello.go
|
||||
> package main
|
||||
>
|
||||
> import (
|
||||
> "fmt"
|
||||
>
|
||||
> "example/user/importpackage/importpackage"
|
||||
> )
|
||||
>
|
||||
> func main() {
|
||||
> fmt.Println(importpackage.ReverseRunes("!oG ,olleH"))
|
||||
> }
|
||||
go run hello.go
|
||||
```
|
Loading…
Reference in New Issue
Block a user