The Go Programming Language

Source file doc/progs/sum.go

     1	// Copyright 2009 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	package main
     6	
     7	import "fmt"
     8	
     9	func sum(a []int) int { // returns an int
    10		s := 0
    11		for i := 0; i < len(a); i++ {
    12			s += a[i]
    13		}
    14		return s
    15	}
    16	
    17	func main() {
    18		s := sum([3]int{1, 2, 3}[:]) // a slice of the array is passed to sum
    19		fmt.Print(s, "\n")
    20	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.