|
| 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 | +} |
0 commit comments