print("Hello, world!")
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
var myVariable = 42
myVariable = 50
let myConstant = 42
var myVariable = 42
myVariable = 50
const myConstant = 42
let explicitDouble: Double = 70
const explicitDouble float64 = 70
let label = "The width is "
let width = 94
let widthLabel = label + String(width)
const label = "The width is "
const width = 94
var widthLabel = label + strconv.Itoa(width)
let apples = 3
let oranges = 5
let fruitSummary = "I have \(apples + oranges) " +
"pieces of fruit."
const apples = 3
const oranges = 5
fruitSummary := fmt.Sprintf("I have %d pieces of fruit.", apples + oranges)
let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..count {
print("Person \(i + 1) is called \(names[i])")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
names := [4]string{"Anna", "Alex", "Brian", "Jack"}
for i, value := range(names) {
fmt.Printf("Person %v is called %v\n", (i + 1), value)
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
for i := 1; i <= 5; i++ {
fmt.Printf("%d times 5 is %d", i, i*5)
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
var shoppingList = ["catfish", "water",
"tulips", "blue paint"]
shoppingList[1] = "bottle of water"
var shoppingList = []string{"catfish", "water",
"tulips", "blue paint"}
shoppingList[1] = "bottle of water"
var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
var occupations = map[string]string{
"Malcolm": "Captain",
"Kaylee": "Mechanic",
}
occupations["Jayne"] = "Public Relations"
let emptyArray = [String]()
let emptyDictionary = [String: Float]()
let emptyArrayNoType = []
var (
emptyArray []string
emptyDictionary = make(map[interface{}]interface{})
emptyArrayNoType []interface{}
)
func greet(name: String, day: String) -> String {
return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")
func greet(name, day string) string {
return fmt.Sprintf("Hello %v, today is %v.", name, day)
}
func main() {
greet("Bob", "Tuesday")
}
func getGasPrices() -> (Double, Double, Double) {
return (3.59, 3.69, 3.79)
}
func getGasPrices() (float64, float64, float64) {
return 3.59, 3.69, 3.79
}
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
sumOf(42, 597, 12)
func sumOf(numbers ...int) int {
var sum = 0
for _, number := range(numbers) {
sum += number
}
return sum
}
func main() {
sumOf(42, 597, 12)
sumOf([]int{42, 597, 12}...)
}
func makeIncrementer() -> (Int -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
}
var increment = makeIncrementer()
increment(7)
func makeIncrementer() func(int) int {
return func (number int) int {
return 1 + number
}
}
func main() {
increment := makeIncrementer()
increment(7)
}
var numbers = [20, 19, 7, 12]
numbers.map({ number in 3 * number })
mapFunc := func(slice interface{}, fn func(a interface{}) interface{}) interface{} {
val := reflect.ValueOf(slice)
out := reflect.MakeSlice(reflect.TypeOf(slice), val.Len(), val.Cap())
for i := 0; i < val.Len(); i++ {
out.Index(i).Set(
reflect.ValueOf(fn(val.Index(i).Interface())),
)
}
return out.Interface()
}
a := mapFunc([]int{1, 2, 3, 4}, func(val interface{}) interface{} {
return val.(int) * 3
})
sort([1, 5, 3, 12, 2]) { $0 > $1 }
sort.Sort(sort.Reverse(sort.IntSlice([]int{1, 5, 3, 12, 2})))
def area(width: Int, height: Int) -> Int {
return width * height
}
area(width: 10, height: 10)
class Shape {
var numberOfSides = 0
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
type Shape struct {
numberOfSides int
}
func (p *Shape) simpleDescription() string {
return fmt.Sprintf("A shape with %d sides.", p.numberOfSides)
}
var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
var shape = new(Shape)
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
class NamedShape {
var numberOfSides: Int = 0
var name: String
init(name: String) {
self.name = name
}
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
class Square: NamedShape {
var sideLength: Double
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 4
}
func area() -> Double {
return sideLength * sideLength
}
override func simpleDescription() -> String {
return "A square with sides of length
\(sideLength)."
}
}
let test = Square(sideLength: 5.2, name: "square")
test.area()
test.simpleDescription()
type NamedShape struct {
numberOfSides int
name string
}
func NewNamedShape(name string) *NamedShape {
return &NamedShape{
name: name,
}
}
func (p *NamedShape) SimpleDescription() string {
return fmt.Sprintf("A shape with %d sides.", p.numberOfSides)
}
type Square struct {
*NamedShape
sideLength float64
}
func NewSquare(sideLength float64, name string) *Square {
return &Square{
NamedShape: NewNamedShape(name),
sideLength: sideLength,
}
}
func (p *Square) Area() float64 {
return p.sideLength * p.sideLength
}
func (p *Square) SimpleDescription() string {
return fmt.Sprintf("A square with sides of length %d.", p.sideLength)
}
func main() {
a := NewSquare(5.2, "square")
a.Area()
a.SimpleDescription()
}
var movieCount = 0
var songCount = 0
for item in library {
if item is Movie {
++movieCount
} else if item is Song {
++songCount
}
}
var movieCount = 0
var songCount = 0
for _, item := range(library) {
if _, ok := item.(Movie); ok {
movieCount++
} else if _, ok := item.(Song); ok {
songCount++
}
}
for object in someObjects {
let movie = object as Movie
print("Movie: '\(movie.name)', dir. \(movie.director)")
}
for object := range someObjects {
movie := object.(Movie)
fmt.Printf("Movie: '%s', dir. %s", movie.name, movie.director)
}
protocol Nameable {
func name() -> String
}
func f(x: T) {
print("Name is " + x.name())
}
type Nameabler interface {
func Name() string
}
func F(x Nameabler) {
fmt.Println("Name is " + x.Name())
}
extension Double {
var km: Double { return self * 1_000.0 }
var m: Double { return self }
var cm: Double { return self / 100.0 }
var mm: Double { return self / 1_000.0 }
var ft: Double { return self / 3.28084 }
}
let oneInch = 25.4.mm
print("One inch is \(oneInch) meters")
// prints "One inch is 0.0254 meters"
let threeFeet = 3.ft
print("Three feet is \(threeFeet) meters")
// prints "Three feet is 0.914399970739201 meters"
type double float64
func (d double) km() double { return d * 1000 }
func (d double) m() double { return d }
func (d double) cm() double { return d / 100 }
func (d double) mm() double { return d / 1000 }
func (d double) ft() double { return d / 3.28084 }
func main() {
var oneInch = double(25.4).mm()
fmt.Printf("One inch is %v meters\n", oneInch)
// prints "One inch is 0.0254 meters"
var threeFeet = double(3).ft()
fmt.Printf("Three feet is %v meters\n", threeFeet)
// prints "Three feet is 0.914399970739201 meters"
}