-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathhi.go
197 lines (159 loc) · 3.94 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// 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"
"github.com/go-python/gopy/_examples/structs"
)
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,
}
}
// PersonAsIface creates a new person as a PersIface interface
func PersonAsIface(name string, age int) PersIface {
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) {
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
}
func (p *Person) GetName() string {
return p.Name
}
func (p *Person) GetAge() int {
return p.Age
}
func (p *Person) SetName(n string) {
p.Name = n
}
func (p *Person) SetAge(age int) {
p.Age = age
}
func (p *Person) SetFmS2(s2 structs.S2) {
p.Age = s2.Public
}
func (p *Person) SetFmS2Ptr(s2 *structs.S2) {
p.Age = s2.Public
}
func (p *Person) ReturnS2Ptr() *structs.S2 {
s2 := &structs.S2{Public: p.Age}
return s2
}
// 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
// PersIface is an interface into the person type.
type PersIface interface {
// GetName returns the name of the person
GetName() string
// GetAge returns the age of the person
GetAge() int
// SetName sets name
SetName(n string)
// SetAge sets age
SetAge(age int)
// Greet sends greetings
Greet() string
}