Tracks
/
Go
Go
/
Exercises
/
Savings Account
Savings Account

Savings Account

Work In Progress
Learning Exercise

Introduction

In Go, a constant is a simple, unchanging value assigned to a name with the const keyword:

const myWebsite = "Exercism"

Such constants are untyped, but they are given a default type based on their syntax when a type is required, such as when they are passed to a method. Typed constants can be created by explicitly adding a type:

const myWebsite string = "Exercism"

Go does not have enums like some other languages, but does have a predeclared identifier called iota for creating enumerated constants. Constants in a block are implicitly repeated:

const (
    a = 9
    b
    c
    d = iota
    e
    f
    g
)
fmt.Print(a, b, c, d, e, f, g)
// Output: 9 9 9 3 4 5 6

Instructions

In this exercise you will establish some constants to be used in the operation of a bank.

You have five tasks:

1. Represent the fixed interest rate on a loan

Create the FixedInterestRate untyped numeric constant to hold the value of 5% (5/100), then implement the GetFixedInterestRate function to return it:

GetFixedInterestRate()
// => 0.05

2. Represent the number of days in a year

Create the DaysPerYear constant with type int to hold the value 365, then implement the GetDaysPerYear function to return it:

GetDaysPerYear()
// => 365

3. Represent the months of the year

In a block, use the Go enumerator to create twelve untyped numeric constants to hold the values 1 through 12 for the months of the year. Name them Jan, Feb, Mar, etc., then implement the GetMonth function to return the value for a given month:

GetMonth(Jan)
// => 1

4. Represent an account number

Create the AccountNo untyped string constant to hold the value XF348IJ, then implement the GetAccountNumber function to return it:

GetAccountNumber()
// => "XF348IJ"
Edit via GitHub The link opens in a new window or tab
Go Exercism

Ready to start Savings Account?

Sign up to Exercism to learn and master Go with 34 concepts, 140 exercises, and real human mentoring, all for free.