05-type_conversions/

directory
v0.0.0-...-4ea1ca7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2015 License: Apache-2.0

README

Type Conversions - Language Syntax

Go is a strict type safe language so typed values must be explicitly converted from one type to the other. Untyped constants on the other hand can be implicitly converted by the compiler. These values exist in a kind system and have more flexibility. In these examples we will learn about named types and how the time package uses them, in conjuction with constants, to provide both a flexible and type safe API.

Notes

  • Declare a type based on another type including built-in and user defined types.
  • Learn about explicit and implicit conversions.
  • See the power of constants and their use in the standard library.
  • Use typed constants are part of the API for your package functions and methods.

Code Review

Declare, create and initialize named types (Go Playground)

Named types in the standard library (Go Playground)

Conversions I (Go Playground)

Conversions II (Go Playground)

Exercises

Exercise 1

Part A Declare a named type called counter with a base type of int. Declare and initialize a variable of this named type to its zero value. Display the value of this variable and the variables type.

Part B Declare a new variable of the named type assign it the value of 10. Display the value.

Part C Declare a variable of the same base type as your named typed. Attempt to assign the value of your named type variable to your new base type variable. Does the compiler allow the assignment?

Template (Go Playground) | Answer (Go Playground)


Ardan Labs Ardan Studios GoingGo Blog


All material is licensed under the Apache License Version 2.0, January 2004.

Directories

Path Synopsis
Sample program to show how to declare and use a named type.
Sample program to show how to declare and use a named type.
Sample program to show a idiomatic use of named types from the standard library and how they work in concert with other Go concepts.
Sample program to show a idiomatic use of named types from the standard library and how they work in concert with other Go concepts.
Sample program to show a idiomatic use of named types from the standard library and how they work in concert with other Go concepts.
Sample program to show a idiomatic use of named types from the standard library and how they work in concert with other Go concepts.
Sample program to show a idiomatic use of named types from the standard library and how they work in concert with other Go concepts.
Sample program to show a idiomatic use of named types from the standard library and how they work in concert with other Go concepts.
exercises
exercise1
Declare a named type called counter with a base type of int.
Declare a named type called counter with a base type of int.
template1
Declare a named type called counter with a base type of int.
Declare a named type called counter with a base type of int.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL