Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book 1.5. Stack—Custom Types with Methods

1.5. Stack—Custom Types with Methods

Although Go supports object-oriented programming it provides neither classes nor inheritance (is-a relationships). Go does support the creation of custom types, and Go makes aggregation (has-a relationships) extremely easy. Go also allows for the complete separation of a type’s data from its behavior, and supports duck typing. Duck typing is a powerful abstraction mechanism that means that values can be handled (e.g., passed to functions), based on the methods they provide, regardless of their actual types. The terminology is derived from the phrase, “If it walks like a duck, and quacks like a duck, it is a duck”. All of this produces a more flexible and powerful alternative to the classes and inheritance approach—but does require those of us used to the more traditional approach to make some significant conceptual adjustments to really benefit from Go’s object orientation.

Go represents data using the fundamental built-in types such as keyword!!struct bool, int, and string, or by aggregations of types using structs.6 Go’s custom types are based on the fundamental types, or on structs, or on other custom types. (We will see some simple examples later in this chapter; §1.7, → 40.)

Go supports both named and unnamed custom types. Unnamed types with the same structure can be used interchangeably; however, they cannot have any methods. (We will discuss this more fully in §6.4, → 273.) Any named custom type can have methods and these methods together constitute the type’s interface. Named custom types—even with the same structure—are not interchangeable. (Throughout the book any reference to a “custom type” means a named custom type, unless stated otherwise.)

An interface is a type that can be formally defined by specifying a particular set of methods. Interfaces are abstract and cannot be instantiated. A concrete (i.e., noninterface) type that has the methods specified by an interface fulfills the interface, that is, values of such a concrete type can be used as values of the interface’s type as well as of their own actual type. Yet no formal connection need be established between an interface and a concrete type that provides the methods specified by the interface. It is sufficient for a custom type to have the interface’s methods for it to satisfy that interface. And, of course, a type can satisfy more than one interface simply by providing all the methods for all the interfaces we want it to satisfy.

The empty interface (i.e., the interface that has no methods) is specified as interface{}. 7 Since the empty interface makes no demands at all (because it doesn’t require any methods), it can stand for any value (in effect like a pointer to any value), whether the value is of a built-in type or is of a custom type. (Go’s pointers and references are explained later; §4.1, → 138.) Incidentally, in Go terminology we talk about types and values rather than classes and objects or instances (since Go has no classes).

Function and method parameters can be of any built-in or custom type—or of any interface type. In the latter case this means that a function can have a parameter that says, for example, “pass a value that can read data”, regardless of what that value’s type actually is. (We will see this in practice shortly; §1.6, → 29.)

Chapter 6 covers all of these matters in detail and presents many examples to ensure that the ideas are understood. For now, let’s just look at a very simple custom type—a stack—starting with how values are created and used, and then looking at the implementation of the custom type itself.

We will start with the output produced by a simple test program:


Each item was popped from the custom stack and printed on its own line.

The simple test program that produced this output is stacker/stacker.go. Here are the imports it uses:


The fmt package is part of Go’s standard library, but the stack package is a local package specific to the stacker application. A Go program or package’s imports are first searched for under the GOPATH path or paths, and then under GOROOT. In this particular case the program’s source code is in $HOME/goeg/src/stacker/stacker.go and the stack package is in $HOME/goeg/src/stacker/stack/stack.go. The go tool will build both of them so long as the GOPATH is (or includes) the path $HOME/goeg/.

Import paths are specified using Unix-style “/”s, even on Windows. Every local package should be stored in a directory with the same name as the package. Local packages can have their own packages (e.g., like path/filepath), in exactly the same way as the standard library. (Creating and using custom packages is covered in Chapter 9.)

Here’s the simple test program’s main() function that produced the output:


The function begins by declaring the haystack variable of type stack.Stack. It is conventional in Go to always refer to types, functions, variables, and other items in packages using the syntax pkg.item, where pkg is the last (or only) component of the package’s name. This helps prevent name collisions. We then push some items onto the stack and then pop them off and print each one until there are no more left.

One amazingly convenient aspect of our custom stack is that despite Go’s strong typing, we are not limited to storing homogeneous items (items all of the same type), but can freely mix heterogeneous items (items of various types). This is because the stack.Stack type simply stores interface{} items (i.e., values of any type) and doesn’t care what their types actually are. Of course, when those items are used, then their type does matter. Here, though, we only use the fmt.Println() function and this uses Go’s introspection facilities (from the reflect package) to discover the types of the items it is asked to print. (Reflection is covered in a later chapter; §9.4.9, → 425.)

Another nice Go feature illustrated by the code is the forloop with no conditions. This is an infinite loop, so in most situations we will need to provide a means of breaking out of the loop—for example, using a break statement as here, or a return statement. We will see an additional for syntax in the next example (§1.6, → 29); the complete range of for syntaxes is covered in Chapter 5.

Go functions and methods can return a single value or multiple values. It is conventional in Go to report errors by returning an error value (of type error) as the last (or only) value returned by a function or method. The custom stack. Stack type respects this convention.

Now that we have seen the custom stack.Stack type in use we are ready to review its implementation (in file stacker/stack/stack.go).


The file starts conventionally by specifying its package name. Then it imports other packages that it needs—in this case just one, errors.

When we define a named custom type in Go what we are doing is binding an identifier (the type’s name) to a new type that has the same underlying representation as an existing (built-in or custom) type—and which is treated by Go as different from the underlying representation. Here, the Stack type is a new name for a slice (i.e., a reference to a variable-length array) of interface{} values—and is considered to be different from a plain []interface{}.

Because all Go types satisfy the empty interface, values of any type can be stored in a Stack.

The built-in collection types (maps and slices), communication channels (which can be buffered), and strings, can all return their length (or buffer size) using the built-in len() function. Similarly, slices and channels can also report their capacity (which may be greater than the length being used) using the built-in cap() function. (All of Go’s built-in functions are listed in Table 5.1; → 185, with cross-references to where they are covered; slices are covered in Chapter 4; §4.2, → 146.) It is conventional for custom collection types—our own, and those in the Go standard library—to support corresponding Len() and Cap() methods when these make sense.

Since the Stack type uses a slice for its underlying representation it makes sense to provide Stack.Len() and Stack.Cap() methods for it.


Both functions and methods are defined using the func keyword.However,in the case of methods the type of value to which the method applies is written after the func keyword and before the method’s name, enclosed in parentheses. After the function or method’s name comes a—possibly empty—parenthesized list of comma-separated parameters (each written in the form variableName type). After the parameters comes the function or method’s opening brace (if it has no return value), or a single return value (e.g., as a type name such as the int returned by the Stack.Len() method shown here), or a parenthesized list of return values, followed by an opening brace.

In most cases a variable name for the value on which the method is called is also given—as here where we have used the name stack (and with no conflict with the package’s name). The value on which the method is called is known in Go terminology as the receiver. 8

In this example the type of the receiver is Stack, so the receiver is passed by value. This means that any changes made to the receiver would be made on a copy of the original value and in effect lost. This is no problem for methods that don’t modify the receiver, such as the Stack.Len() method shown here.

The Stack.Cap() method is almost identical to the Stack.Len() method (and so is not shown). The only difference is that the Stack.Cap() method returns the cap() rather than the len() of the receiver stack. The source code also includes a Stack.IsEmpty() method, but this is so similar to Stack.Len()—it just returns a bool indicating whether the stack’s len() equals 0—that again it isn’t shown.


The Stack.Push() method is called on a pointer to a Stack (explained in a moment), and is passed a value (x) of any type. The built-in append() function takes a slice and one or more values and returns a (possibly new) slice which has the original slice’s contents, plus the given value or values as its last element or elements. (See §4.2.3, → 154.)

If the stack has previously had items popped from it (→ 28), the underlying slice’s capacity is likely to be greater than its length, so the push could be very cheap: simply a matter of putting the x item into the len(stack) position and increasing the stack’s length by one.

The Stack.Push() method always works (unless the computer runs out of memory), so we don’t need to return an error value to indicate success or failure.

If we want to modify a value we must make its receiver a pointer.9 A pointer is a variable that holds the memory address of another value. One reason that pointers are used is for efficiency—for example, if we have a value of a large type it is much cheaper to pass a pointer to the value as a parameter than to pass the value itself. Another use is to make a value modifiable. For example, when a variable is passed into a function the function gets a copy of the value (e.g., the stack passed into the stack.Len() function; 25←). This means that if we make any changes to the variable inside the function, they will have no effect on the original value. If we need to modify the original value—as here where we want to append to the stack—we must pass a pointer to the original value, and then inside the function we can modify the value that the pointer points to.

A pointer is declared by preceding the type name with a star (i.e., an asterisk, *). So here, in the Stack.Push() method, the stack variable is of type *Stack, that is, the stack variable holds a pointer to a Stack value and not an actual Stack value. We can access the actual Stack value that the pointer points to by dereferencing the pointer—this simply means that we access the value the pointer points to. Dereferencing is done by preceding the variable name with a star. So here, when we write stack we are referring to a pointer to a Stack (i.e., to a *Stack), and when we write *stack we are dereferencing the pointer, that is, referring to the actual Stack that the pointer points to.

So, in Go (and C and C++ for that matter), the star is overloaded to mean multiplication (when between a pair of numbers or variables, e.g., x*y), pointer declaration (when preceding a type name, e.g., z*MyType), and pointer dereference (when preceding a pointer variable’s name, e.g., *z). Don’t worry too much about these matters for now: Go’s pointers are fully explained in Chapter 4.

Note that Go’s channels, maps, and slices are all created using the make() function, and make() always returns a reference to the value it created. References behave very much like pointers in that when they are passed to functions any changes made to them inside the function affect the original channel, map, or slice. However, references don’t need to be dereferenced, so in most cases there’s no need to use stars with them. But if we want to modify a slice inside a func tion or method using append() (as opposed to simply changing one of its existing items), then we must either pass the slice by pointer, or return the slice (and set the original slice to the function or method’s return value), since append() sometimes returns a different slice reference than the one it was passed.

The Stack type uses a slice for its representation and therefore Stack values can be used with functions that operate on a slice, such as append() and len(). Nonetheless, Stack values are values in their own right, distinct from their representation, so they must be passed by pointer if we want to modify them.


The Stack.Top() method returns the item at the top of the stack (the item that was added last) and a nil error value; or a nil item and a non-nil error value, if the stack is empty. The stack receiver is passed by value since the stack isn’t modified.

The error type is an interface type (§6.3, → 263) which specifies a single method, Error() string. In general, Go’s library functions return an error as their last (or only) return value to indicate success (where error is nil) or failure. Here, we have made our Stack type work like a standard library type by creating a new error value using the errors package’s errors.New() function.

Go uses nil for zero pointers (and for zero references); that is, for pointers that point to nothing and for references that refer to nothing.10 Such pointers should be used only in conditions or assignments; methods should not normally be called on them.

Constructors are never called implicitly in Go. Instead Go guarantees that when a value is created it is always initialized to its zero value. For example, numbers are initialized to 0, strings to the empty string, pointers to nil, and the fields inside structs are similarly initialized. So there is no uninitialized data in Go, thus eliminating a major source of errors that afflicts many other programming languages. If the zero value isn’t suitable we can write a construction function—and call it explicitly—as we do here to create a new error. It is also possible to prevent values of a type being created without using a constructor function, as we will see in Chapter 6.

If the stack is nonempty we return its topmost value and a nilerror value. Since Go uses 0-based indexing the first element in a slice or array is at position 0 and the last element is at position len(sliceOrArray)-1.

There is no formality when returning more than one value from a function or method; we simply list the types we are returning after the function or method’s name and ensure that we have at least one return statement that has a corresponding list of values.


The Stack.Pop() method is used to remove and return the top (last added) item from the stack. Like the Stack.Top() method it returns the item and a nil error, or if the stack is empty, a nil item and a non-nil error.

The method must have a receiver that is a pointer since it modifies the stack by removing the returned item. For syntactic convenience, rather than referring to *stack (the actual stack that the stack variable points to) throughout the method, we assign the actual stack to a local variable (theStack), and work with that variable instead. This is quite cheap, because *stack is pointing to a Stack, which uses a slice for its representation, so we are really assigning little more than a reference to a slice.

If the stack is empty we return a suitable error. Otherwise we retrieve the stack’s top (last) item and store it in a local variable (x). Then we take a slice of the stack (which itself is a slice). The new slice has one less element than the original and is immediately set to be the value that the stack pointer points to. And at the end, we return the retrieved value and a nil error. We can reasonably expect any decent Go compiler to reuse the slice, simply reducing the slice’s length by one, while leaving its capacity unchanged, rather than copying all the data to a new slice.

The item to return is retrieved using the [] index operator with a single index (➊); in this case the index of the slice’s last element.

The new slice is obtained by using the [] slice operator with an index range (➋). An index range has the form first:end. If first is omitted—as here—0 is assumed, and if end is omitted, the len() of the slice is assumed. The slice thus obtained has elements with indexes from and including the first up to and excluding the end. So in this case, by specifying the last index as one less than the length, we slice up to the last but one element, effectively removing the last element from the slice. (Slice indexing is covered in Chapter 4; §4.2.1, → 151.)

In this example we used Stack receivers rather than pointers (i.e., of type *Stack) for those methods that don’t modify the Stack. For custom types with lightweight representations (say, a few ints or strings), this is perfectly reasonable. But for heavyweight custom types it is usually best to always use pointer receivers since a pointer is much cheaper to pass (typically a simple 32-or 64-bit value), than a large value, even for methods where the value isn’t modified.

A subtle point to note regarding pointers and methods is that if we call a method on a value, and the method requires a pointer to the value it is called on, Go is smart enough to pass the value’s address rather than a copy of the value (providing the value is addressable; §6.2.1, → 256). Correspondingly, if we call a method on a pointer to a value, and the method requires a value, Go is smart enough to dereference the pointer and give the method the pointed-to value.11

As this example illustrates, creating custom types in Go is generally straightforward, and doesn’t involve the cumbersome formalities that many other languages demand. Go’s object-oriented features are covered fully in Chapter 6.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020