Skip to Main Content
Level Up Your Web Apps With Go
book

Level Up Your Web Apps With Go

by Mal Curtis
April 2015
Intermediate to advanced content levelIntermediate to advanced
260 pages
5h 17m
English
SitePoint
Content preview from Level Up Your Web Apps With Go

Chapter 2Go Types Explored

In the last chapter, we were introduced to the basic Go types and syntax. In this chapter we’ll go a little deeper into some of the types, and how to define your own types and interfaces. We’ll also cover how Go lets us use third-party libraries in our software with ease.

Custom Types

While you can’t add your own methods to built-in types, you can create your own types from other types. A common example of this is a ByteSize type, which extends float64 and provides a custom formatted string to represent a size in bytes such as 2 kilobytes or 3.14 megabytes:

const ( KB = 1024 MB = KB * 1024 GB = MB * 1024 TB = GB * 1024 PB = TB * 1024 ) type ByteSize float64 func (b ByteSize) String() string { switch { case b >= PB: ...
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

Go: Building Web Applications

Go: Building Web Applications

Nathan Kozyra, Mat Ryer

Publisher Resources

ISBN: 9781457192845Errata Page