Skip to content

Commit 3f2feb7

Browse files
committed
golang: add README.md
1 parent 7eccf8b commit 3f2feb7

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
2+
## Go
3+
4+
### Versions
5+
6+
- New versions every 6 months, Feb and Aug
7+
- Each major Go release is supported until there are two newer major releases (~1 year)
8+
- It is intended that programs written to the Go 1 specification will continue to compile and run correctly, unchanged, over the lifetime of that specification. The APIs may grow, acquiring new packages and features, but not in a way that breaks existing Go 1 code. <https://go.dev/doc/go1compat>
9+
- go1.25.5 released 2025-12-02
10+
- go1.25.0 released 2025-08-12
11+
- go1.24.0 released 2025-02-11
12+
- go1.23.0 released 2024-08-13 (out of support)
13+
14+
### Considerations
15+
16+
- Designed to improve programming productivity in an era of multicore, networked machines and large codebases.
17+
18+
- Designers were primarily motivated by their shared dislike of C++.
19+
20+
- Go "interfaces" (like static duck typing) are simple and useful
21+
22+
- Modules seem like an afterthought <https://go.dev/blog/v2-go-modules>
23+
24+
### Tips
25+
26+
- $GOPATH defaults to $HOME/go
27+
28+
- Module cache is kept in $GOPATH/pkg/mod, not in source tree
29+
30+
- Debian install
31+
32+
```
33+
download https://go.dev/dl/go1.25.5.linux-amd64.tar.gz
34+
sudo tar -C /usr/local -xzf go1.25.5.linux-amd64.tar.gz
35+
Add /usr/local/go/bin to PATH
36+
```
37+
38+
### Basic build/test loop
39+
40+
```
41+
go mod init example.com/helloworld
42+
go mod tidy # download deps
43+
go build
44+
go test
45+
```
46+
47+
### Third-party packages
48+
49+
- <https://pkg.go.dev/>
50+
51+
- Adding to project
52+
53+
```
54+
// add import to source
55+
import (
56+
"rsc.io/quote"
57+
)
58+
59+
$ go mod tidy
60+
```
61+
62+
### Native Interoperability
63+
64+
<https://pkg.go.dev/cmd/cgo> <https://dev.to/metal3d/understand-how-to-use-c-libraries-in-go-with-cgo-3dbn> <https://github.com/jbuberel/buildmodeshared>
65+
66+
- Go uses C code in comments before `import "C"` to include C headers and/or arbitrary C code
67+
68+
```
69+
//// these comments are compiled into C code
70+
// #cgo LDFLAGS: -luuid
71+
//
72+
// #include <uuid/uuid.h>
73+
// #include <stdio.h>
74+
// #include <stdlib.h>
75+
//
76+
// // create a uuid function in C to return a uuid char*
77+
// char* _go_uuid() {
78+
// uuid_t uuid;
79+
// uuid_generate_random(uuid);
80+
// char *str = malloc(37);
81+
// uuid_unparse_lower(uuid, str);
82+
// printf("C code in comment\n");
83+
// return str;
84+
// }
85+
import "C"
86+
87+
// uuid generates a UUID using the C shared library.
88+
// It returns a Go string.
89+
func uuid() string {
90+
return C.GoString(C._go_uuid())
91+
}
92+
```
93+
94+
### Websockets example
95+
96+
```
97+
import (
98+
"fmt"
99+
"log"
100+
"github.com/gorilla/websocket"
101+
)
102+
103+
func main() {
104+
// Dial the WebSocket server
105+
var wsURL = "ws://corefx-net-http11.azurewebsites.net/WebSocket/EchoWebSocket.ashx"
106+
conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
107+
if err != nil {
108+
log.Fatal("Dial error:", err)
109+
}
110+
defer conn.Close()
111+
112+
fmt.Println("Connected to", wsURL)
113+
114+
// Send message to server
115+
err = conn.WriteMessage(websocket.TextMessage, []byte("Hello Websocket"))
116+
if err != nil {
117+
log.Fatal("Write error:", err)
118+
}
119+
120+
// Read response from server
121+
_, response, err := conn.ReadMessage()
122+
if err != nil {
123+
log.Fatal("Read error:", err)
124+
}
125+
126+
fmt.Printf("Server: %s\n", string(response))
127+
}
128+
```
129+
130+
### Cross-platform compile from linux
131+
132+
<https://rakyll.org/cross-compilation/>
133+
134+
- If using all go code and no C libraries (i.e. no CGO)
135+
136+
```
137+
go tool dist list
138+
GOOS=linux GOARCH=amd64 go build -o helloworld_linux
139+
GOOS=darwin GOARCH=amd64 go build -o helloworld_darwin
140+
GOOS=windows GOARCH=amd64 go build -o helloworld.exe
141+
```
142+
143+
- If using C libraries (i.e. with CGO)
144+
145+
```
146+
# must have cross-compilers installed
147+
GO_ENABLED=1 \
148+
CC=x86_64-w64-mingw32-gcc \
149+
CXX=x86_64-w64-mingw32-g++ \
150+
GOOS=windows GOARCH=amd64 go build -o helloworld.exe
151+
```
152+
153+
### Introspection
154+
155+
Built-in <https://go.dev/blog/laws-of-reflection>

0 commit comments

Comments
 (0)