Skip to content
🚧 The site is under active development as such some content may be incomplete or missing. 🚧

Go from TypeScript

Use your TypeScript knowledge to learn Go

Know TypeScript? You’re Halfway to Go! πŸ‘‡


interface Item {
label: string;
price: number;
}
function calculatePrice(items: Item[]): number {
let price = 0;
for (const [_, item] of items.entries()) {
price += item.price;
}
return price;
}
let items: Item[] = [
{ label: "item1", price: 10 },
{ label: "item2", price: 20 },
{ label: "item3", price: 30 }
];
let totalPrice = calculatePrice(items);
console.log(`Total price: ${totalPrice}`);

package main
import "fmt"
type Item struct {
Label string
Price int
}
func calculatePrice(items []Item) int {
var price int = 0
for _, item := range items {
price += item.Price
}
return price
}
func main() {
var items = []Item{
{Label: "item1", Price: 10},
{Label: "item2", Price: 20},
{Label: "item3", Price: 30},
}
var totalPrice = calculatePrice(items)
fmt.Printf("Total price: %d\n", totalPrice)
}