Skip to Content
Go: Building Web Applications
book

Go: Building Web Applications

by Nathan Kozyra, Mat Ryer
August 2016
Beginner to intermediate
665 pages
14h 11m
English
Packt Publishing
Content preview from Go: Building Web Applications

Using Go with unit testing

As with many of the basic and intermediate development and deployment requirements you may have, Go comes with a built-in application for handling unit tests.

The basic premise behind testing is that you create your package and then create a testing package to run against the initial application. The following is a very basic example:

mathematics.go
package mathematics

func Square(x int) int {
  
  return x * 3
}
mathematics_test.go
package mathematics

import
(
  "testing"
)

func Test_Square_1(t *testing.T) {
  if Square(2) != 4 {
    t.Error("Square function failed one test")
  }
}

A simple Go test in that subdirectory will give you the response you're looking for. While this was admittedly simple—and purposefully flawed—you can probably ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Hands-On Full Stack Development with Go

Hands-On Full Stack Development with Go

Mina Andrawos

Publisher Resources

ISBN: 9781787123496