Skip to content

Instantly share code, notes, and snippets.

@nitinstp23
Last active June 23, 2025 15:30
Show Gist options
  • Save nitinstp23/81dee3a1e58cfe83099025ab54e44b46 to your computer and use it in GitHub Desktop.
Save nitinstp23/81dee3a1e58cfe83099025ab54e44b46 to your computer and use it in GitHub Desktop.
package main
import "fmt"
type Question struct {
description string
qType string
options []string
}
func printQuiz(questions []Question) {
for _, question := range questions {
switch question.qType {
case "text":
fmt.Println(question.description)
fmt.Println("Answer: ____________________")
case "boolean":
fmt.Println(question.description)
for i, option := range question.options {
fmt.Printf("%d. %s\n", i+1, option)
}
case "multipleChoice":
fmt.Println(question.description)
for i, option := range question.options {
fmt.Printf("%d. %s\n", i+1, option)
}
}
fmt.Println()
}
}
func main() {
questions := []Question{
{
description: "What is your name?",
qType: "text",
},
{
description: "Do you like programming?",
qType: "boolean",
options: []string{"True", "False"},
},
{
description: "What is your favorite programming language?",
qType: "multipleChoice",
options: []string{"GoLang", "Python", "Java", "Typescript"},
},
}
printQuiz(questions)
}
@nitinstp23
Copy link
Author

Refactored code following SOLID design

package main

import "fmt"

// Question interface defines the contract for all question types
type Question interface {
	Print() // Each question type knows how to print itself
}

// TextQuestion represents a text-based question
type TextQuestion struct {
	description string
}

func (t *TextQuestion) Print() {
	fmt.Println(t.description)
	fmt.Println("Answer: ____________________")
	fmt.Println()
}

// BooleanQuestion represents a true/false question
type BooleanQuestion struct {
	description string
	options     []string
}

func (b *BooleanQuestion) Print() {
	fmt.Println(b.description)
	for i, option := range b.options {
		fmt.Printf("%d. %s\n", i+1, option)
	}
	fmt.Println()
}

// MultipleChoiceQuestion represents a multiple choice question
type MultipleChoiceQuestion struct {
	description string
	options     []string
}

func (m *MultipleChoiceQuestion) Print() {
	fmt.Println(m.description)
	for i, option := range m.options {
		fmt.Printf("%d. %s\n", i+1, option)
	}
	fmt.Println()
}

// Quiz manages a collection of questions
type Quiz struct {
	questions []Question
}

func NewQuiz() *Quiz {
	return &Quiz{
		questions: make([]Question, 0),
	}
}

func (q *Quiz) AddQuestion(question Question) {
	q.questions = append(q.questions, question)
}

func (q *Quiz) PrintQuiz() {
	for _, question := range q.questions {
		question.Print() // Polymorphism - each question prints itself
	}
}

func main() {
	quiz := NewQuiz()

	quiz.AddQuestion(&TextQuestion{
		description: "What is your name?",
	})

	quiz.AddQuestion(&BooleanQuestion{
		description: "Do you like programming?",
		options:     []string{"True", "False"},
	})

	quiz.AddQuestion(&MultipleChoiceQuestion{
		description: "What is your favorite programming language?",
		options:     []string{"GoLang", "Python", "Java", "Typescript"},
	})

	quiz.PrintQuiz()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment