Skip to content

Commit 1cdd86a

Browse files
committed
golang helloworld
1 parent 9546875 commit 1cdd86a

File tree

6 files changed

+165
-0
lines changed

6 files changed

+165
-0
lines changed

.github/workflows/golang.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Go Build and Test
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- name: Set up Go
16+
uses: actions/setup-go@v5
17+
with:
18+
go-version: '1.25'
19+
20+
- name: Get dependencies
21+
run: |
22+
cd runtime/golang/helloworld
23+
go mod tidy
24+
25+
- name: Build
26+
run: |
27+
cd runtime/golang/helloworld
28+
go build -v
29+
30+
- name: Test
31+
run: |
32+
cd runtime/golang/helloworld
33+
go test -v
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
helloworld

runtime/golang/helloworld/go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module jitx.com/deploytest1/runtime/golang/helloworld
2+
3+
go 1.25.5
4+
5+
require (
6+
github.com/gorilla/websocket v1.5.3
7+
rsc.io/quote v1.5.2
8+
)
9+
10+
require (
11+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c // indirect
12+
rsc.io/sampler v1.3.0 // indirect
13+
)

runtime/golang/helloworld/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
2+
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
3+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=
4+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
5+
rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=
6+
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
7+
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
8+
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

runtime/golang/helloworld/main.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//go:build linux || darwin || windows
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"log"
8+
9+
"github.com/gorilla/websocket"
10+
"rsc.io/quote"
11+
)
12+
13+
//// use an external shared C library
14+
//
15+
// #cgo LDFLAGS: -luuid
16+
//
17+
// #include <uuid/uuid.h>
18+
// #include <stdio.h>
19+
// #include <stdlib.h>
20+
//
21+
// // create a uuid function in C to return a uuid char*
22+
// char* _go_uuid() {
23+
// uuid_t uuid;
24+
// uuid_generate_random(uuid);
25+
// char *str = malloc(37);
26+
// uuid_unparse_lower(uuid, str);
27+
// printf("C code in comment\n");
28+
// return str;
29+
// }
30+
import "C"
31+
32+
// uuid generates a UUID using the C shared library.
33+
// It returns a Go string.
34+
func uuid() string {
35+
return C.GoString(C._go_uuid())
36+
}
37+
38+
func main() {
39+
fmt.Println("Hello, World!")
40+
fmt.Println(quote.Hello())
41+
42+
myuuid := uuid() // this is a go string now
43+
fmt.Println(myuuid)
44+
45+
// Dial the WebSocket server
46+
var wsURL = "ws://corefx-net-http11.azurewebsites.net/WebSocket/EchoWebSocket.ashx"
47+
conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
48+
if err != nil {
49+
log.Fatal("Dial error:", err)
50+
}
51+
defer conn.Close()
52+
53+
fmt.Println("Connected to", wsURL)
54+
55+
// Send message to server
56+
err = conn.WriteMessage(websocket.TextMessage, []byte("Hello Websocket"))
57+
if err != nil {
58+
log.Fatal("Write error:", err)
59+
}
60+
61+
// Read response from server
62+
_, response, err := conn.ReadMessage()
63+
if err != nil {
64+
log.Fatal("Read error:", err)
65+
}
66+
67+
fmt.Printf("Server: %s\n", string(response))
68+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"github.com/gorilla/websocket"
5+
"rsc.io/quote"
6+
"testing"
7+
)
8+
9+
// test package function
10+
func TestExternalPackage(t *testing.T) {
11+
output := quote.Hello()
12+
if output == "" {
13+
t.Error("External package rsc.io/quote returned empty string")
14+
}
15+
}
16+
17+
// test websocket client
18+
func TestWebsocketClient(t *testing.T) {
19+
// Dial the WebSocket server
20+
var wsURL = "ws://corefx-net-http11.azurewebsites.net/WebSocket/EchoWebSocket.ashx"
21+
conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
22+
if err != nil {
23+
t.Error("Dial error:", err)
24+
}
25+
defer conn.Close()
26+
27+
// Send message to server
28+
err = conn.WriteMessage(websocket.TextMessage, []byte("Hello Websocket"))
29+
if err != nil {
30+
t.Error("Write error:", err)
31+
}
32+
33+
// Read response from server
34+
_, response, err := conn.ReadMessage()
35+
if err != nil {
36+
t.Error("Read error:", err)
37+
}
38+
39+
if string(response) != "Hello Websocket" {
40+
t.Error("websocket did not echo input")
41+
}
42+
}

0 commit comments

Comments
 (0)