Skip to content

Commit a40b093

Browse files
committed
docs: update simple example in README
Update the Go and TypeScript code examples in the README to include goroutines, channels, and function literals, matching the changes in example/simple/main.go. Signed-off-by: Christian Stewart <christian@aperture.us>
1 parent 4f0eed7 commit a40b093

1 file changed

Lines changed: 46 additions & 10 deletions

File tree

README.md

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,21 +234,39 @@ func main() {
234234
default:
235235
println("Switch default")
236236
}
237+
238+
// Goroutines and Channels
239+
println("\nGoroutines and Channels:")
240+
ch := make(chan string)
241+
go func() {
242+
println("Goroutine: Sending message")
243+
ch <- "Hello from goroutine!"
244+
}()
245+
msg := <-ch
246+
println("Main goroutine: Received message:", msg)
247+
248+
// Function Literals
249+
println("\nFunction Literals:")
250+
add := func(x, y int) int {
251+
return x + y
252+
}
253+
sum = add(5, 7)
254+
println("Function literal result:", sum)
237255
}
238256
```
239257

240258
Generated with `goscript compile .`:
241259

242260
```typescript
243-
import * as goscript from "@go/builtin";
261+
import * as goscript from "@go/builtin"
244262

245263
class MyStruct {
246264
// MyInt is a public integer field, initialized to zero.
247-
public MyInt: number = 0;
265+
public MyInt: number = 0
248266
// MyString is a public string field, initialized to empty string.
249-
public MyString: string = "";
267+
public MyString: string = ""
250268
// myBool is a private boolean field, initialized to false.
251-
private myBool: boolean = false;
269+
private myBool: boolean = false
252270

253271
// GetMyString returns the MyString field.
254272
public GetMyString(): string {
@@ -262,8 +280,8 @@ class MyStruct {
262280
return m.myBool
263281
}
264282

265-
constructor(init?: Partial<MyStruct>) { if (init) Object.assign(this, init as any); }
266-
public clone(): MyStruct { return Object.assign(Object.create(MyStruct.prototype) as MyStruct, this); }
283+
constructor(init?: Partial<MyStruct>) { if (init) Object.assign(this, init as any) }
284+
public clone(): MyStruct { return Object.assign(Object.create(MyStruct.prototype) as MyStruct, this) }
267285
}
268286

269287
// NewMyStruct creates a new MyStruct instance.
@@ -275,7 +293,7 @@ function vals(): [number, number] {
275293
return [1, 2]
276294
}
277295

278-
export function main(): void {
296+
export async function main(): Promise<void> {
279297
console.log("Hello from GoScript example!")
280298

281299
// Basic arithmetic
@@ -288,17 +306,17 @@ export function main(): void {
288306
console.log("Comparisons:", a == b, a != b, a < b, a > b, a <= b, a >= b)
289307

290308
// string(rune) conversion
291-
let r: number = 'X';
309+
let r: number = 'X'
292310
let s = String.fromCharCode(r)
293311
console.log("string('X'):", s)
294312

295313
// 'y'
296-
let r2: number = 121;
314+
let r2: number = 121
297315
let s2 = String.fromCharCode(r2)
298316
console.log("string(121):", s2)
299317

300318
// '√'
301-
let r3: number = 0x221A;
319+
let r3: number = 0x221A
302320
let s3 = String.fromCharCode(r3)
303321
console.log("string(0x221A):", s3)
304322

@@ -371,6 +389,24 @@ export function main(): void {
371389
console.log("Switch default")
372390
break
373391
}
392+
393+
// Goroutines and Channels
394+
console.log("\nGoroutines and Channels:")
395+
let ch = goscript.makeChannel<string>(0)
396+
queueMicrotask(async () => {
397+
console.log("Goroutine: Sending message")
398+
await ch.send("Hello from goroutine!")
399+
})
400+
let msg = await ch.receive()
401+
console.log("Main goroutine: Received message:", msg)
402+
403+
// Function Literals
404+
console.log("\nFunction Literals:")
405+
let add = (x: number, y: number): number => {
406+
return x + y
407+
}
408+
sum = add(5, 7)
409+
console.log("Function literal result:", sum)
374410
}
375411
```
376412

0 commit comments

Comments
 (0)