2008-09-11 11:21:02 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2009-03-18 15:09:16 -06:00
|
|
|
import "fmt"
|
|
|
|
|
2010-09-09 21:53:18 -06:00
|
|
|
func sum(a []int) int { // returns an int
|
2009-12-15 16:29:53 -07:00
|
|
|
s := 0
|
2008-09-11 11:21:02 -06:00
|
|
|
for i := 0; i < len(a); i++ {
|
2009-10-13 13:37:04 -06:00
|
|
|
s += a[i]
|
2008-09-11 11:21:02 -06:00
|
|
|
}
|
2009-10-13 13:37:04 -06:00
|
|
|
return s
|
2008-09-11 11:21:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
2010-09-09 21:53:18 -06:00
|
|
|
s := sum([3]int{1, 2, 3}[:]) // a slice of the array is passed to sum
|
2009-12-15 16:29:53 -07:00
|
|
|
fmt.Print(s, "\n")
|
2008-09-11 11:21:02 -06:00
|
|
|
}
|