Lab Two
21 Sep 2025
Intrinsic Data Types (Go Version)
- Given the following enumeration concept, write a Go program to output the values associated with each constant.
Use aniota-basedconstblock.
const (
RED = iota
YELLOW
AMBER = YELLOW
GREEN
)
- Write a program in which you define variables of the following kinds
- integer
- pointer to integer
- another variable that refers to the same integer (simulate a reference)
- constant integer
Perform the following actions on the variables
- assign the value
5to the integer - increment the integer through the pointer
- increment the integer through the reference variable
- output the integer and verify that its value is
7
What happens if you attempt to change the value of the constant?
Output the decimal values of the following literals:
0xf3f20437'a'
- Write a program which reads a single line of hyphen-connected words and outputs the words one per line, with a count of the number of characters in each.
Example:
Enter words: hello-this-is-a-line-of-input
[5] hello
[4] this
[2] is
[1] a
[4] line
[2] of
[5] input
Use only primitive byte/character slices and pointer-style indexing (no strings.Split or other helpers).
- Using the Go
stringtype, create a type definition for an array of 10 strings.
Using the type, create an array and then read 10 string objects from standard input, and output the string with the largest size.
- Repeat the previous exercise using a type alias.
Which syntax do you find more readable?
Optional Question
- Write a 2-dimensional noughts and crosses (tic-tac-toe) game implemented with either a 1D or 2D array.
The game should display the board using anXcharacter for a cross,Ofor a nought, and?for a free location.
The program should first display an empty board (full of ?s) and repeatedly prompt two users for their next move.
Requests for Xs and Os are made alternately.
Moves may be indicated by a two-digit code (0 0 for top-left, 2 2 for bottom-right) or any other helpful mechanism.
Example:
? ? ?
? ? ?
? ? ?
X move: 1 1
? ? ?
? X ?
? ? ?
O move:
This exercise will be extended in later chapters.