From 6ffed384620b55a59442dfd1003b546220f6059f Mon Sep 17 00:00:00 2001 From: asandikci Date: Wed, 30 Aug 2023 21:14:10 +0300 Subject: [PATCH] Initial Commit --- README.md | 40 +++++++++++++++++++++++++++++++++++++ cmd/app/main.go | 15 ++++++++++++++ go.mod | 3 +++ pkg/mypkg/main.go | 5 +++++ pkg/mypkg/symbol_linux.go | 7 +++++++ pkg/mypkg/symbol_windows.go | 7 +++++++ 6 files changed, 77 insertions(+) create mode 100644 README.md create mode 100644 cmd/app/main.go create mode 100644 go.mod create mode 100644 pkg/mypkg/main.go create mode 100644 pkg/mypkg/symbol_linux.go create mode 100644 pkg/mypkg/symbol_windows.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..4cce51a --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# Go Crosscompile + +# Explanation +symbol() function in pkg/mypkg/symbol_linux.go and symbol_windows.go is different. When running and building program, GOOS choses right one. for more information run these in cmd/app/ directory: +```sh + go build . + ./app + # Output: + #1+2=linux + #a/b/c +``` + +--- + +```sh + env GOOS=windows GOARCH=amd64 go build . + ./app.exe + # Output: + # -Some random wine logs if running on linux with wine- + #1-2=windows + #a\b\c +``` +--- +```sh + file app* + # see information about executables +``` +# HELP NEEDED +VSCode gives redecleration error when using same name as function names. How to solve this? +maybe see https://github.com/golang/go/issues/29202 ? + +# Before trying +- make sure you have installed **wine** in your linux system + - or use a windows system too + +# Resources +- https://www.digitalocean.com/community/tutorials/customizing-go-binaries-with-build-tags +- https://www.digitalocean.com/community/tutorials/building-go-applications-for-different-operating-systems-and-architectures +- https://www.digitalocean.com/community/tutorials/how-to-build-go-executables-for-multiple-platforms-on-ubuntu-20-04 +- https://opensource.com/article/21/1/go-cross-compiling diff --git a/cmd/app/main.go b/cmd/app/main.go new file mode 100644 index 0000000..3e8607f --- /dev/null +++ b/cmd/app/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "fmt" + "path/filepath" + + "git.aliberksandikci.com.tr/asandikci/go-crosscompile/pkg/mypkg" +) + +func main() { + s := filepath.Join("a", "b", "c") + fmt.Println(mypkg.Symbol("1", "2")) + + fmt.Println(s) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bd4eb54 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.aliberksandikci.com.tr/asandikci/go-crosscompile + +go 1.21.0 diff --git a/pkg/mypkg/main.go b/pkg/mypkg/main.go new file mode 100644 index 0000000..24e6f2e --- /dev/null +++ b/pkg/mypkg/main.go @@ -0,0 +1,5 @@ +package mypkg + +func Symbol(a, b string) string { + return symbol(a, b) +} diff --git a/pkg/mypkg/symbol_linux.go b/pkg/mypkg/symbol_linux.go new file mode 100644 index 0000000..8f64d8f --- /dev/null +++ b/pkg/mypkg/symbol_linux.go @@ -0,0 +1,7 @@ +// go:build linux + +package mypkg + +func symbol(a, b string) string { + return a + "+" + b + "=linux" +} diff --git a/pkg/mypkg/symbol_windows.go b/pkg/mypkg/symbol_windows.go new file mode 100644 index 0000000..391d262 --- /dev/null +++ b/pkg/mypkg/symbol_windows.go @@ -0,0 +1,7 @@ +// go:build windows + +package mypkg + +func symbol(a, b string) string { + return a + "-" + b + "=windows" +}