2015-01-30 17:02:15 +00:00
|
|
|
// Copyright 2015 The go-python 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 hi exposes a few Go functions to be wrapped and used from Python.
|
|
|
|
package hi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Hi prints hi from Go
|
|
|
|
func Hi() {
|
|
|
|
fmt.Printf("hi from go\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hello prints a greeting from Go
|
|
|
|
func Hello(s string) {
|
|
|
|
fmt.Printf("hello %s from go\n", s)
|
|
|
|
}
|
|
|
|
|
2015-01-30 17:44:58 +00:00
|
|
|
// Concat concatenates two strings together and returns the resulting string.
|
|
|
|
func Concat(s1, s2 string) string {
|
|
|
|
return s1 + s2
|
|
|
|
}
|
|
|
|
|
2015-01-30 17:02:15 +00:00
|
|
|
// Add returns the sum of its arguments.
|
|
|
|
func Add(i, j int) int {
|
|
|
|
return i + j
|
|
|
|
}
|
2015-07-24 14:16:31 +00:00
|
|
|
|
|
|
|
// Person is a simple struct
|
|
|
|
type Person struct {
|
|
|
|
Name string
|
|
|
|
Age int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPerson creates a new Person value
|
|
|
|
func NewPerson(name string, age int) Person {
|
|
|
|
return Person{
|
|
|
|
Name: name,
|
|
|
|
Age: age,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 14:54:44 +00:00
|
|
|
// NewPersonWithAge creates a new Person with a specific age
|
|
|
|
func NewPersonWithAge(age int) Person {
|
|
|
|
return Person{
|
|
|
|
Name: "stranger",
|
|
|
|
Age: age,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 14:57:53 +00:00
|
|
|
// NewActivePerson creates a new Person with a certain amount of work done.
|
|
|
|
func NewActivePerson(h int) (Person, error) {
|
|
|
|
var p Person
|
|
|
|
err := p.Work(h)
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
2015-07-24 14:16:31 +00:00
|
|
|
func (p Person) String() string {
|
|
|
|
return fmt.Sprintf("hi.Person{Name=%q, Age=%d}", p.Name, p.Age)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Greet sends greetings
|
|
|
|
func (p *Person) Greet() string {
|
2015-07-27 16:46:30 +00:00
|
|
|
return p.greet()
|
|
|
|
}
|
|
|
|
|
|
|
|
// greet sends greetings
|
|
|
|
func (p *Person) greet() string {
|
2015-07-24 14:16:31 +00:00
|
|
|
return fmt.Sprintf("Hello, I am %s", p.Name)
|
|
|
|
}
|
2015-07-29 09:57:51 +00:00
|
|
|
|
|
|
|
// Work makes a Person go to work for h hours
|
|
|
|
func (p *Person) Work(h int) error {
|
|
|
|
fmt.Printf("working...\n")
|
|
|
|
if h > 7 {
|
|
|
|
return fmt.Errorf("can't work for %d hours!", h)
|
|
|
|
}
|
2015-07-29 12:15:31 +00:00
|
|
|
fmt.Printf("worked for %d hours\n", h)
|
2015-07-29 09:57:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-07-29 12:50:02 +00:00
|
|
|
|
|
|
|
// Salary returns the expected gains after h hours of work
|
|
|
|
func (p *Person) Salary(h int) (int, error) {
|
|
|
|
if h > 7 {
|
|
|
|
return 0, fmt.Errorf("can't work for %d hours!", h)
|
|
|
|
}
|
|
|
|
return h * 10, nil
|
|
|
|
}
|