This document discusses developing cross-platform applications with Go. It covers how Go is designed to be cross-platform, using interfaces to abstract platform specifics, and build constraints to control compilation. It also discusses cross compilation, system calls, file system behavior differences between Linux and Windows, and how to access Windows APIs from Go.
2. Programlama Dilleri
Aktif olarak Go ile geliştirme yapmakla
birlikte uzun yıllar PHP backend
developer olarak proje geliştirdim.
İletişim Bilgisi
Eposta : yakuter@gmail.com
Twitter: @yakuter
İş / Görev
Binalyze isimli Enterprise Forensics
yazılım şirketinde Senior Software
Architect olarak çalışmaktayım.
Tecrübe/Bilgi
Yaklaşık 15+ yıldır yazılım geliştirme ile
ilgilenmekte olup, şu anda işletim
sistemleri üzerinde olay sonrası delillerin
toplanması için yazılım geliştirmekteyim.
Erhan YAKUT
(yakuter)
Biyografi
Ben Kimim?
3. Golang Cross Platform Support
Cross Platform with Golang
The Go language was designed to be a systems language usable across
multiple platforms. It has a number of built-in language and build features
that make it ideal for easy cross-platform development.
https://pkg.go.dev/cmd/go#section-documentation
5. Build Constraints
Build Constraints
Build constraints are a language construct that can be used to control the
platforms and architectures on which a file will be compiled. They come in
two flavors:
1. Filename-based
<name>[_GOOS][_GOARCH].<extension>
2. Comment at top of file
Commas = AND
Spaces = OR
//go:build (linux && 386) || (darwin && !cgo)
// +build linux,386 darwin,!cgo
-> go tool dist list
-> go tool dist list -json
-> https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63
6. Cross Compilation
Cross Compilation
Because of some non-Go pieces (library and drivers), we avoid doing this.
set GOOS=windows
set GOARCH=386
go build .
CGO
sudo apt-get install gcc-multilib
sudo apt-get install gcc-mingw-w64
-> GOOS=windows GOARCH=386 CGO_ENABLED=1 CXX=i686-w64-mingw32-g++ CC=i686-w64-
mingw32-gcc go build
https://dave.cheney.net/2015/08/22/cross-compilation-with-go-1-5
7. Syscall
What is System Call in Operating System?
In computing, a system call (commonly abbreviated to syscall) is the programmatic way
in which a computer program requests a service from the kernel of the operating system
on which it is executed.
https://www.guru99.com/system-call-operating-system.html
Types of System calls
Here are the five types of System Calls in OS:
1. Process Control
2. File Management
3. Device Management
4. Information Maintenance
5. Communications
10. Hostname
Linux
hostname, err := os.Hostname()
Windows
hostname, err := os.Hostname()
windows.GetComputerNameEx()
----
syscall.ComputerName()
GetComputerName retrieves only the NetBIOS name of the
local computer. To retrieve the DNS host name, DNS domain
name, or the fully qualified DNS name, call the
GetComputerNameEx function.
https://docs.microsoft.com/en-
us/windows/win32/api/winbase/nf-winbase-
getcomputernamew
https://cs.opensource.google/go/x/sys/+/03aa0b5f:windows/s
yscall_windows.go;drc=94396e421777fa1601dc71542cd5cb0c6
7fe4664;l=211
11. Filesystem Behaviour
Works in Linux / Fails in Windows
file, _ := os.Open(“/home/user/test.log”)
_ = os.Remove(file.Name())
You should first close the file, then remove in Windows.
12. Filesystem Redirection
What is Windows Filesystem Redirection?
On 64-bit Windows operating systems the %WINDIR%system32 directory is reserved
for 64-bit applications and by default any attempt by a 32-bit application to reference
the %WINDIR%system32 directory will be redirected to the %WINDIR%SysWow64
directory.
Disable Filesystem Redirection
pWow64Disable = kernel32.NewProc("Wow64DisableWow64FsRedirection")
pWow64Revert = kernel32.NewProc("Wow64RevertWow64FsRedirection")
13. Windows API
What is Windows Filesystem Redirection
Windows APIs are published via DLLs delivered with each installation of the
Windows OS.
https://docs.microsoft.com/en-us/windows/win32/apiindex/api-index-portal
To Load a DLL in Go, you can use syscall.NewLazyDLL or syscall.LoadLibrary .
After the DLL is loaded (or lazy-loaded), you then must get a reference to the the
Procedure using dll.NewProc("ProcName") .
var (
kernel32DLL = syscall.NewLazyDLL("kernel32.dll")
procOpenProcess = kernel32DLL.NewProc("OpenProcess")
)
15. Registry
Windows Registry
The Windows Registry is a hierarchical database that stores low-level
settings for the Microsoft Windows operating system and for
applications that opt to use the registry. The kernel, device drivers,
services, Security Accounts Manager, and user interfaces can all use the
registry.
16. Signals
What is Signals?
Signals are software interrupts sent to a program to indicate that an
important event has occurred. The events can vary from user requests
to illegal memory access errors. Some signals, such as the interrupt
signal, indicate that a user has asked the program to do something
that is not in the usual flow of control. Examples:
SIGINT, SIGKILL, SIGTERM
17. Signals
Signal in Windows
var hevent windows.Handle
hevent, _ = windows.CreateEvent(nil, 0, 0, nil)
---
windows.SetEvent(e.handle)
--
syscall.WaitForSingleObject(hevent, syscall.INFINITE)
https://pkg.go.dev/golang.org/x/sys/windows
https://docs.microsoft.com/en-us/cpp/c-runtime-
library/reference/signal?view=msvc-170