PageRenderTime 60ms CodeModel.GetById 45ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/go/reference/runme.go

#
Go | 71 lines | 34 code | 18 blank | 19 comment | 2 complexity | 632359b8db363e0fbd88b8fca2e68bf7 MD5 | raw file
 1// This example illustrates the manipulation of C++ references in Java.
 2
 3package main
 4
 5import (
 6	"fmt"
 7	. "./example"
 8)
 9
10func main() {
11	fmt.Println("Creating some objects:")
12	a := NewVector(3, 4, 5)
13	b := NewVector(10, 11, 12)
14
15	fmt.Println("    Created ", a.Print())
16	fmt.Println("    Created ", b.Print())
17
18	// ----- Call an overloaded operator -----
19
20	// This calls the wrapper we placed around
21	//
22	//      operator+(const Vector &a, const Vector &)
23	//
24	// It returns a new allocated object.
25
26	fmt.Println("Adding a+b")
27	c := Addv(a, b)
28	fmt.Println("    a+b = " + c.Print())
29
30	// Because addv returns a reference, Addv will return a
31	// pointer allocated using Go's memory allocator.  That means
32	// that it will be freed by Go's garbage collector, and we can
33	// not use DeleteVector to release it.
34
35	c = nil
36
37	// ----- Create a vector array -----
38
39	fmt.Println("Creating an array of vectors")
40	va := NewVectorArray(10)
41	fmt.Println("    va = ", va)
42
43	// ----- Set some values in the array -----
44
45	// These operators copy the value of Vector a and Vector b to
46	// the vector array
47	va.Set(0, a)
48	va.Set(1, b)
49
50	va.Set(2, Addv(a, b))
51
52	// Get some values from the array
53
54	fmt.Println("Getting some array values")
55	for i := 0; i < 5; i++ {
56		fmt.Println("    va(", i, ") = ", va.Get(i).Print())
57	}
58
59	// Watch under resource meter to check on this
60	fmt.Println("Making sure we don't leak memory.")
61	for i := 0; i < 1000000; i++ {
62		c = va.Get(i % 10)
63	}
64
65	// ----- Clean up ----- This could be omitted. The garbage
66	// collector would then clean up for us.
67	fmt.Println("Cleaning up")
68	DeleteVectorArray(va)
69	DeleteVector(a)
70	DeleteVector(b)
71}