Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Go sample #516

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions samples-test/samples/Go/language_stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"fmt"
"sort"
)

// Language represents a programming language
type Language struct {
// Name of the language
Name string
// Color associated with the language
Color string
// Rank of the language in popularity
Rank int
// Year the language was created
YearCreated int
// Features of the language
Features []string
}

// LanguageStats represents a collection of programming languages
type LanguageStats struct {
// List of languages
languages []Language
}

// AddLanguage adds a new language to the collection
func (ls *LanguageStats) AddLanguage(lang Language) {
ls.languages = append(ls.languages, lang)
}

// SortByRank sorts the languages by rank
func (ls *LanguageStats) SortByRank() {
sort.Slice(ls.languages, func(i, j int) bool {
return ls.languages[i].Rank < ls.languages[j].Rank
})
}

func main() {
stats := LanguageStats{}

// Add Go
stats.AddLanguage(Language{
Name: "Go",
Color: "Blue",
Rank: 8,
YearCreated: 2009,
Features: []string{"Concurrent", "Compiled", "Static typing"},
})

// Add Python
stats.AddLanguage(Language{
Name: "Python",
Color: "Yellow and Blue",
Rank: 3,
YearCreated: 1991,
Features: []string{"Dynamic typing", "Interpreted", "Object-oriented"},
})

// Add JavaScript
stats.AddLanguage(Language{
Name: "JavaScript",
Color: "Yellow",
Rank: 1,
YearCreated: 1995,
Features: []string{"Dynamic typing", "Interpreted", "Prototype-based"},
})

stats.SortByRank()

fmt.Println("Programming Languages by Popularity Rank:")
fmt.Println("----------------------------------------")

for _, lang := range stats.languages {
fmt.Printf("%s (Rank: %d)\n", lang.Name, lang.Rank)
fmt.Printf("Created in: %d\n", lang.YearCreated)
fmt.Printf("Features: %v\n", lang.Features)
fmt.Println("----------------------------------------")
}
}