/*------------------------------------------------------- * variables.go *------------------------------------------------------- * Illustrates use of primitive types and formatted IO * Jim Fawcett, CSE775 - Distributed Objects, Spring 2016 *-------------------------------------------------------- */ package main import ( "fmt" ) /* * Notes: * - functions can return tuples * - variables don't become invalid until they have no references, * e.g., it's ok to return a pointer to local data * - new is a function that accepts a type and returns a pointer to * a new instance of that type * - there is no managed heap, even though all data is subject to * garbage collection * - the language decides where to store data, not the developer */ func weird() (*int, *float32) { p := new(int) *p++; var f float32 = 3.1415927 return p, &f } func main() { var i int = -7 // int, int8, int16, int32, int64 var u uint = 15 // uint, uint8, uint16, uint32, uint64 var f float32 = 3.1415927 // float32, float64 var c complex64 = 3.5 + 2.0i // complex64, complex128 var s string = "this is a string" var qs string = "this printed as a quoted string" fmt.Printf("\n vars are:\n %d %v, %g, %g\n %s, %qs\n", i, u, f, c, s, qs) pi, pf := weird() // := is intialized declaration, = is an assignment // if you use var, don't use := fmt.Printf("\n return values of weird(): %v, %v", *pi, *pf) fmt.Printf("\n their addresses are: %v, %v\n", pi, pf) weirder := weird pi, pf = weirder(); fmt.Printf("\n return values of weirder(): %v, %v\n", *pi, *pf) // must occupy just one line // https://golang.org/pkg/fmt/ Printf formatting }