In a previous post I wrote that if a language has a standard way of making projects, then that should be taught right away, along with automated tests. Then I wrote a post in which I went over making a simple project with Go.
I realized I skipped something important: adding external dependencies. I will re-use the project from the previous post. You should complete the steps in that post before starting this one.
The project already has two packages. This time I will add two files in one of our existing packages. Each file will use a different module. I will also include a test for each.
I picked a couple of project off of the Awesome Go github page.
The first one is stringy (link to module on pkg.go.dev here).
cd spearhead
emacs morestrings/use_stringy.go
Here is the contents of the file:
package morestrings
import (
"fmt"
"github.com/gobeam/stringy"
)
func UseReplaceLast(root_string, old_s, new_s string ) string {
fmt.Println("in morestrings/UseReplaceLast")
replaceLast := stringy.New(root_string)
return replaceLast.ReplaceLast(old_s, new_s)
}
I am only using the “ReplaceLast” function. Notice the line importing “github.com/gobeam/stringy”.
Next I run “go build”:
go build
morestrings/use_stringy.go:5:2: no required module provides package github.com/gobeam/stringy; to add it:
go get github.com/gobeam/stringy
I will add code to spearhead.go to invoke my function:
fmt.Println("about to call morestrings.UseReplaceLast('this is my string, working with strings, i love strings', 'string', 'rope') : ",
morestrings.UseReplaceLast("this is my string, working with strings, i love strings", "string", "rope"))
Before I run “go get”, here is what is in the go.mod file:
more go.mod
module info/shelfunit/spearhead
go 1.18
Now I run “go get”:
go get github.com/gobeam/stringy
go: added github.com/gobeam/stringy v0.0.5
Here is go.mod:
module info/shelfunit/spearhead
go 1.18
require github.com/gobeam/stringy v0.0.5 // indirect
There are no issues running “go build” and running the executable:
go build
ericm@latitude:~/github/goapps/spearhead$ ./spearhead
Hello, world.
about to call numberstuff.AddOneToSum( 5, 6): 12
about to call numberstuff.SubtractOneFromSum( 5, 6 ): 10
about to call numberstuff.AddOneToProduct( 5, 6): 31
about to call numberstuff.SubtractOneFromProduct( 5, 6 ): 29
about to call morestrings.ReturnStringTwice("twice"): twicetwice
in morestrings/UseReplaceLast
about to call morestrings.UseReplaceLast('this is my string, working with strings, i love strings', 'string', 'rope') : this is my string, working with strings, i love ropes
The next step is to make a test file: morestrings/use_stringy_test.go
package morestrings
import (
"fmt"
"testing"
)
func TestUseReplaceLast(t *testing.T) {
fmt.Println( "Starting TestUseReplaceLast" )
cases := []struct {
root, old, new, want string
}{
{"Hello, world, i love the world, dog meets world", "world", "food", "Hello, world, i love the world, dog meets food"},
{"learning go, will go places, gonna be big", "go", "wa", "learning go, will go places, wanna be big"},
{"I love life, go live your life, go create life", "life", "lice", "I love life, go live your life, go create lice"},
}
for _, c := range cases {
fmt.Println( "c.root: ", c.root, ", c.old: ", c.old, ", c.new: ", c.new, ", c.want: ", c.want )
got := UseReplaceLast(c.root, c.old, c.new)
if got != c.want {
t.Errorf("UseReplaceLast(%q, %q, %q) == %q, want %q", c.root, c.old, c.new, got, c.want)
}
}
fmt.Println( "Ending TestUseReplaceLast" )
}
When I run “go test -v ./morestrings/” we get all the tests for this module. I will only share the output of the test I just added:
=== RUN TestUseReplaceLast
Starting TestUseReplaceLast
c.root: Hello, world, i love the world, dog meets world , c.old: world , c.new: food , c.want: Hello, world, i love the world, dog meets food
in morestrings/UseReplaceLast
c.root: learning go, will go places, gonna be big , c.old: go , c.new: wa , c.want: learning go, will go places, wanna be big
in morestrings/UseReplaceLast
c.root: I love life, go live your life, go create life , c.old: life , c.new: lice , c.want: I love life, go live your life, go create lice
in morestrings/UseReplaceLast
Ending TestUseReplaceLast
--- PASS: TestUseReplaceLast (0.00s)
If I run “go test -v ./morestrings/use_stringy_test.go”, I get an error:
# command-line-arguments [command-line-arguments.test]
morestrings/use_stringy_test.go:19:16: undefined: UseReplaceLast
FAIL command-line-arguments [build failed]
FAIL
I am sure some Golang experts are shaking their heads because I did something wrong. As far as I know, while the “go test” command can run tests using a directory as an argument, it does not work as well when you specify individual files.
To test only the test I just created, I can run this command:
go test -v ./morestrings -run TestUseReplaceLast
I suppose if you wanted to run multiple tests in a specific file, you would need to give them all a name that is common to the tests in that file, but also unique to that group of tests. So you need to be careful when naming your tests. The argument after the “run” is a regular expression. If I wanted to run the tests from the last post in ./numberstuff/multiplication_enhancements_test.go, I would use this command:
go test -v ./numberstuff/ -run Test.*Product
To run the tests in numberstuff/addition_enhancements_test.go, I would use this command:
go test -v ./numberstuff/ -run Test.*Sum.*
The next package I will use is strutil (link to module on pkg.go.dev here). The file using this module is morestrings/use_strutil.go:
package morestrings
import (
"fmt"
"github.com/ozgio/strutil"
)
func UseCountWords(root_string string ) int {
fmt.Println("in morestrings/UseCountWords")
return strutil.CountWords(root_string)
}
Again: note there is an import statement for our module. Here is the output of “go build”:
go build
morestrings/use_strutil.go:5:2: no required module provides package github.com/ozgio/strutil; to add it:
go get github.com/ozgio/strutil
So I run the “go get” command:
go get github.com/ozgio/strutil
go: added github.com/ozgio/strutil v0.4.0
Now we have two modules in a block in go.mod:
more go.mod
module info/shelfunit/spearhead
go 1.18
require (
github.com/gobeam/stringy v0.0.5 // indirect
github.com/ozgio/strutil v0.4.0 // indirect
)
I will add a couple of calls to our new function in spearhead.go:
fmt.Println("about to call morestrings.UseCountWords('this is my string with words'): ", morestrings.UseCountWords("this is my string with words"))
root_string := "I am again called upon by the voice of my country to execute the functions of its Chief Magistrate"
fmt.Println("about to call UseCountWords('I am again called upon by the voice of my country to execute the functions of its Chief Magistrate'): ",
morestrings.UseCountWords(root_string))
And again I call “go build” and run the executable:
go build
ericm@latitude:~/github/goapps/spearhead$ ./spearhead
Hello, world.
about to call numberstuff.AddOneToSum( 5, 6): 12
about to call numberstuff.SubtractOneFromSum( 5, 6 ): 10
about to call numberstuff.AddOneToProduct( 5, 6): 31
about to call numberstuff.SubtractOneFromProduct( 5, 6 ): 29
about to call morestrings.ReturnStringTwice("twice"): twicetwice
in morestrings/UseReplaceLast
about to call morestrings.UseReplaceLast('this is my string, working with strings, i love strings', 'string', 'rope') : this is my string, working with strings, i love ropes
in morestrings/UseCountWords
about to call morestrings.UseCountWords('this is my string with words'): 6
in morestrings/UseCountWords
about to call UseCountWords('I am again called upon by the voice of my country to execute the functions of its Chief Magistrate'): 19
I will make a test file in morestrings/use_strutil_test.go:
package morestrings
import (
"fmt"
"testing"
)
func TestUseCountWords(t *testing.T) {
fmt.Println( "Starting TestUseCountWords" )
cases := []struct {
in_string string
want int
}{
{"Among the vicissitudes incident to life no event could have filled me with greater anxieties than that of which the notification was transmitted by your order, and received on the 14th day of the present month", 36},
{"I am again called upon by the voice of my country to execute the functions of its Chief Magistrate", 19},
{"When it was first perceived, in early times, that no middle course for America remained between unlimited submission to a foreign legislature and a total independence of its claims, men of reflection were less apprehensive of danger from the formidable power of fleets and armies they must determine to resist than from those contests and dissensions which would certainly arise concerning the forms of government to be instituted over the whole and over the parts of this extensive country", 79},
}
for _, c := range cases {
fmt.Println( "----- c.in_string: ", c.in_string, ", c.want: ", c.want) // , ", here it is: ", UseCountWords(c.in_string))
got := UseCountWords(c.in_string)
if got != c.want {
t.Errorf("ERROR: UseCountWords(%q) == %d, want %d", c.in_string, got, c.want)
}
}
fmt.Println( "Ending TestUseCountWords" )
}
Here is the result of go test -v ./morestrings -run TestUseCountWords:
=== RUN TestUseCountWords
Starting TestUseCountWords
----- c.in_string: Among the vicissitudes incident to life no event could have filled me with greater anxieties than that of which the notification was transmitted by your order, and received on the 14th day of the present month , c.want: 36
in morestrings/UseCountWords
----- c.in_string: I am again called upon by the voice of my country to execute the functions of its Chief Magistrate , c.want: 19
in morestrings/UseCountWords
----- c.in_string: When it was first perceived, in early times, that no middle course for America remained between unlimited submission to a foreign legislature and a total independence of its claims, men of reflection were less apprehensive of danger from the formidable power of fleets and armies they must determine to resist than from those contests and dissensions which would certainly arise concerning the forms of government to be instituted over the whole and over the parts of this extensive country , c.want: 79
in morestrings/UseCountWords
Ending TestUseCountWords
--- PASS: TestUseCountWords (0.00s)
PASS
ok info/shelfunit/spearhead/morestrings 0.002s
In case you are wondering, while reading through post, what those quotes are, it is my duty to inform you, as any good upstanding citizen would, that they are from the first three inaugural addresses of the heads of government of our great country, whose writing style, which leaves much to be desired by people living in the post-Hemingway era, given that inserting entire thoughts in the middle of sentences can introduce unneeded complexity, I have done my utmost to emulate.
Given the length and content of Washington’s second inaugural address, I get the impression he did not want a second term.
You’re welcome.
Image from Synaxarium of Euthymius of Athos, an 11-century Georgian manuscript, image from Wikimedia, assumed allowed under public domain.