hello.go
and put the following code inside itgo run hello.go
.main
package defined with a main
func inside it. Packages are ways of grouping up related Go code together.func
keyword is how you define a function with a name and a body.import "fmt"
we are importing a package which contains the Println
function that we use to print.fmt.Println
is a side effect (printing to stdout) and the string we send in is our domain.func
but this time we've added another keyword string
in the definition. This means this function returns a string
.hello_test.go
where we are going to write a test for our Hello
functiongo test
in your terminal. If the tests pass, then you are probably using an earlier version of Go. However, if you are using Go 1.16 or later, then the tests will likely not run at all. Instead, you will see an error message like this in the terminal:go mod init hello
in your terminal. That will create a new file with the following contents:go
tools essential information about your code. If you planned to distribute your application, you would include where the code was available for download as well as information about dependencies. For now, your module file is minimal, and you can leave it that way. To read more about modules, you can check out the reference in the Golang documentation. We can get back to testing and learning Go now since the tests should run, even on Go 1.16.go mod init SOMENAME
in each new folder before running commands like go test
or go build
.go test
in your terminal. It should've passed! Just to check, try deliberately breaking the test by changing the want
string.xxx_test.go
Test
t *testing.T
*testing.T
type, you need to import "testing"
, like we did with fmt
in the other filet
of type *testing.T
is your "hook" into the testing framework so you can do things like t.Fail()
when you want to fail.if
varName := value
, which lets us re-use some values in our test for readability.t.Errorf
Errorf
method on our t
which will print out a message and fail the test. The f
stands for format which allows us to build a string with values inserted into the placeholder values %q
. When you made the test fail it should be clear how it works.%q
is very useful as it wraps your values in double quotes.godoc -http :8000
. If you go to localhost:8000/pkg you will see all the packages installed on your system.godoc
command, then maybe you are using the newer version of Go (1.14 or later) which is no longer including godoc
. You can manually install it with go install golang.org/x/tools/cmd/[email protected]
.go test
, you should have a compilation errorHello
to accept an argument.Hello
function to accept an argument of type stringhello.go
will fail to compile because you're not passing an argument. Send in "world" to make it compile.Hello,
commit
the code as it is. We have working software backed by a test."Hello, "
string instance every time Hello
is called.t *testing.T
so that we can tell the test code to fail when we need to.testing.TB
which is an interface that *testing.T
and *testing.B
both satisfy, so you can call helper functions from a test, or a benchmark (don't worry if words like "interface" mean nothing to you right now, it will be covered later).t.Helper()
is needed to tell the test suite that this method is a helper. By doing this when it fails the line number reported will be in our function call rather than inside our test helper. This will help other developers track down problems easier. If you still don't understand, comment it out, make a test fail and observe the test output. Comments in Go are a great way to add additional information to your code, or in this case, a quick way to tell the compiler to ignore a line. You can comment out the t.Helper()
code by adding two forward slashes //
at the beginning of the line. You should see that line turn grey or change to another color than the rest of your code to indicate it's now commented out.if
.Hello
with two arguments rather than one.Hello
Hello
in your other tests and in hello.go
if
here to check the language is equal to "Spanish" and if so change the message"French"
you get "Bonjour, "
switch
if
statements checking a particular value it is common to use a switch
statement instead. We can use switch
to refactor the code to make it easier to read and more extensible if we wish to add more language support later(prefix string)
.prefix
in your function.int
s are 0 and for string
s it is ""
.return
rather than return prefix
.default
in the switch case will be branched to if none of the other case
statements match.Hello, world
?if
, const
and switch
Hello()
to Hello("name")
, to Hello("name", "French")
in small, easy to understand steps.