Skip to content

Commit fc92024

Browse files
committed
feat: add winapi example
1 parent 9502dec commit fc92024

File tree

10 files changed

+102
-5
lines changed

10 files changed

+102
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vscode
22
.idea
3+
.DS_Store

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ docker pull x1unix/go-mingw:latest # or "1.21" for specific Go version
2727
```
2828

2929
> [!TIP]
30-
> Please take a look at [full project build example](example/sqlite-app) before starting to work.
30+
> Please take a look at [examples](example/) before starting to work.
3131
3232
### Building Go applications inside container
3333

@@ -62,7 +62,7 @@ docker run --rm -it -e GOARCH=386 -v /YourPackageSrc:/go/work \
6262
```
6363

6464
> [!TIP]
65-
> See full project build example [here](example/sqlite-app).
65+
> See check project build examples [here](example).
6666
6767
### Go linker flags override
6868

@@ -144,4 +144,3 @@ make image GO_VERSION=1.20
144144

145145
> [!IMPORTANT]
146146
> Replace `1.20` with desired Go version.
147-

example/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Examples
2+
3+
This directory contains various examples of Go projects with CGO and cross-compilation scripts that use this Docker image.
4+
5+
* [hello](hello) - Simple app that uses CGO with WinAPI.
6+
* [sqlite-app](sqlite-app) - Example that uses CGO for SQLite driver.

example/hello/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.vscode
2+
.idea
3+
*.exe
4+
*.db

example/hello/Makefile

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
GO_VERSION := 1.21 # or "latest" for bleeding-edge Go version
2+
GO_BUILD_FLAGS := -x
3+
GOCACHE ?= $(shell go env GOCACHE)
4+
GOPATH ?= $(shell go env GOPATH)
5+
6+
define docker_build
7+
@echo ":: Building for $(1) in MinGW container..."
8+
docker run --rm -it \
9+
-e GOCACHE=/go/.cache \
10+
-e GOARCH=$(1) \
11+
-v "$(GOCACHE)":/go/.cache \
12+
-v "$(GOPATH)/src":/go/src \
13+
-v "$(GOPATH)/pkg":/go/pkg \
14+
-v "$(PWD)":/go/work \
15+
-w /go/work \
16+
x1unix/go-mingw:$(GO_VERSION) \
17+
go build $(GO_BUILD_FLAGS) -o hello-$(1).exe .
18+
endef
19+
20+
.PHONY:all
21+
all: build-amd64 build-i386 build-arm64
22+
23+
.PHONY: build-amd64 build-i386 build-arm64
24+
build-amd64:
25+
$(call docker_build,amd64)
26+
27+
build-i386:
28+
$(call docker_build,386)
29+
30+
build-arm64:
31+
$(call docker_build,arm64)

example/hello/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# CGO Hello World
2+
3+
![demo](demo.png)
4+
5+
This is a simple app with CGO and invokes `MessageBox` function from WinAPI.
6+
7+
Run `make` to cross-compile program.

example/hello/demo.png

35.3 KB
Loading

example/hello/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module hello
2+
3+
go 1.21

example/hello/main.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
/*
4+
#cgo LDFLAGS: -lkernel32
5+
#include <windows.h>
6+
#include <stdio.h>
7+
8+
// Function to show a MessageBox using WinAPI
9+
void hello() {
10+
SYSTEM_INFO si;
11+
ZeroMemory(&si, sizeof(SYSTEM_INFO));
12+
GetSystemInfo(&si);
13+
char* arch;
14+
switch (si.wProcessorArchitecture) {
15+
case PROCESSOR_ARCHITECTURE_AMD64:
16+
arch = "AMD64";
17+
break;
18+
case PROCESSOR_ARCHITECTURE_INTEL:
19+
arch = "x86";
20+
break;
21+
case PROCESSOR_ARCHITECTURE_ARM:
22+
arch = "ARM";
23+
break;
24+
case PROCESSOR_ARCHITECTURE_ARM64:
25+
arch = "ARM64";
26+
break;
27+
case PROCESSOR_ARCHITECTURE_IA64:
28+
arch = "IA";
29+
break;
30+
default:
31+
arch = "Unknown";
32+
break;
33+
}
34+
35+
char message[30];
36+
sprintf(message, "Hello from CGO on %s", arch);
37+
38+
MessageBox(NULL, message, "Hello World", MB_OK);
39+
}
40+
*/
41+
import "C"
42+
import "fmt"
43+
44+
func main() {
45+
fmt.Println("Calling C function to open a MessageBox...")
46+
C.hello()
47+
}

example/sqlite-app/Makefile

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
GO_VERSION := 1.17 # or "latest" for bleeding-edge Go version
1+
GO_VERSION := 1.21 # or "latest" for bleeding-edge Go version
22
GO_BUILD_FLAGS := -x
33
GOCACHE ?= $(shell go env GOCACHE)
44
GOPATH ?= $(shell go env GOPATH)
55

66
define docker_build
77
@echo ":: Building for $(1) in MinGW container..."
88
docker run --rm -it \
9-
-u "$${UID}:$${GID}" \
109
-e GOCACHE=/go/.cache \
1110
-e GOARCH=$(1) \
1211
-v "$(GOCACHE)":/go/.cache \

0 commit comments

Comments
 (0)