← All Exercises

Go Struct Basics

basicgo~15min

Background

Structs are Go primary way to group related data, with methods providing object-oriented behavior.

Problem

Create a Rectangle struct with width and height fields, and methods to compute area and perimeter.

Input

Width and height as float64 values.

Output

A Rectangle with Area() and Perimeter() methods.

Sample tests

Sample 1

Input: width=5, height=3
Expected: area=15, perimeter=16

Your solution

Learning objective

Learn struct definition, method declaration, and value vs pointer receiver semantics.