forked from go-python/gopy
/
hi.go
141 lines (116 loc) · 3 KB
/
hi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// 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"
"github.com/go-python/gopy/_examples/cpkg"
)
const (
Version = "0.1" // Version of this package
Universe = 42 // Universe is the fundamental constant of everything
)
var (
Debug = false // Debug switches between debug and prod
Anon = Person{Age: 1, Name: "<nobody>"} // Anon is a default anonymous person
IntSlice = []int{1, 2} // A slice of ints
IntArray = [2]int{1, 2} // An array of ints
)
// Hi prints hi from Go
func Hi() {
cpkg.Hi()
}
// Hello prints a greeting from Go
func Hello(s string) {
cpkg.Hello(s)
}
// Concat concatenates two strings together and returns the resulting string.
func Concat(s1, s2 string) string {
return s1 + s2
}
// LookupQuestion returns question for given answer.
func LookupQuestion(n int) (string, error) {
if n == 42 {
return "Life, the Universe and Everything", nil
} else {
return "", fmt.Errorf("Wrong answer: %v != 42", n)
}
}
// Add returns the sum of its arguments.
func Add(i, j int) int {
return i + j
}
// 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,
}
}
// NewPersonWithAge creates a new Person with a specific age
func NewPersonWithAge(age int) Person {
return Person{
Name: "stranger",
Age: age,
}
}
// 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
}
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 {
return p.greet()
}
// greet sends greetings
func (p *Person) greet() string {
return fmt.Sprintf("Hello, I am %s", p.Name)
}
// Work makes a Person go to work for h hours
func (p *Person) Work(h int) error {
cpkg.Printf("working...\n")
if h > 7 {
return fmt.Errorf("can't work for %d hours!", h)
}
cpkg.Printf("worked for %d hours\n", h)
return nil
}
// 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
}
// Couple is a pair of persons
type Couple struct {
P1 Person
P2 Person
}
// NewCouple returns a new couple made of the p1 and p2 persons.
func NewCouple(p1, p2 Person) Couple {
return Couple{
P1: p1,
P2: p2,
}
}
func (c *Couple) String() string {
return fmt.Sprintf("hi.Couple{P1=%v, P2=%v}", c.P1, c.P2)
}
// Float is a kind of float32
type Float float32
// Floats is a slice of floats
type Floats []Float
// Eval evals float64
type Eval func(f float64) float64