hello.goand put the following code inside it
go run hello.go.
mainpackage defined with a
mainfunc inside it. Packages are ways of grouping up related Go code together.
funckeyword is how you define a function with a name and a body.
import "fmt"we are importing a package which contains the
Printlnfunction that we use to print.
fmt.Printlnis a side effect (printing to stdout) and the string we send in is our domain.
funcbut this time we've added another keyword
stringin the definition. This means this function returns a
hello_test.gowhere we are going to write a test for our
go testin 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:
gotools 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 SOMENAMEin each new folder before running commands like
go testin your terminal. It should've passed! Just to check, try deliberately breaking the test by changing the
*testing.Ttype, you need to
import "testing", like we did with
fmtin the other file
*testing.Tis your "hook" into the testing framework so you can do things like
t.Fail()when you want to fail.
varName := value, which lets us re-use some values in our test for readability.
Errorfmethod on our
twhich will print out a message and fail the test. The
fstands 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.
godoc -http :8000. If you go to localhost:8000/pkg you will see all the packages installed on your system.
go test, you should have a compilation error
Helloto accept an argument.
Hellofunction to accept an argument of type string
hello.gowill fail to compile because you're not passing an argument. Send in "world" to make it compile.
committhe code as it is. We have working software backed by a test.
"Hello, "string instance every time
t *testing.Tso that we can tell the test code to fail when we need to.
testing.TBwhich is an interface that
*testing.Bboth satisfy, so you can call helper functions from a test, or a benchmark.
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.
Hellowith two arguments rather than one.
Helloin your other tests and in
ifhere to check the language is equal to "Spanish" and if so change the message
ifstatements checking a particular value it is common to use a
switchstatement instead. We can use
switchto refactor the code to make it easier to read and more extensible if we wish to add more language support later
prefixin your function.
ints are 0 and for
strings it is
defaultin the switch case will be branched to if none of the other
Hello("name", "French")in small, easy to understand steps.