Montag, 16. November 2009

trapped go

we create first a file (package) Person
Person.go:

package Person

type Person struct {
vorname string;
name string;
alter int;
}

func NewPerson(v string, n string, a int) *Person {
return &Person{v, n, a}
}

func (pers *Person) String() string {
return "Name: "+pers.name+" Vorname: "+pers.vorname;
}


then the main package with main()

Main.go

package main

import (
prs "./Person";
"fmt";
)


func main() {

person := prs.NewPerson("blue", "blub", 42);


fmt.Printf("person is: \n");
fmt.Printf(person.String());
fmt.Printf("\n");
}


compile and link:
6g Person.go
6g Main.go
6l -o Main Main.6

execute: ./Main
>person is:
>Name: blub Vorname: blue

At first we must import our file as "./Person"

At second the function that are created for outside must begin with Capitalized Character.


package Person

type Person struct {
vorname string;
name string;
alter int;
}

func newPerson(v string, n string, a int) *Person {
return &Person{v, n, a}
}


Person.newPerson(...) doesnot work.

"For clients (importers) of the package, names must
be upper case to be visible: global variables,
functions, types, constants, plus methods and
structure fields for global variables and types.

const hello = "you smell" // package visible
const Hello = "you smell nice" // globally visible
const _Bye = "stinko!" // _ is not upper
"

Keine Kommentare:

Kommentar veröffentlichen