Managing platform-specific code
Back in Chapter 2, The Future According to Fyne, we saw that the Go compiler has built-in support for the conditional inclusion of source files based on a system of environment variables and build tags. As an application adds more functionality, especially from a platform integration perspective, it is possible that the toolkit will not provide all of the functionality you are looking for. When this happens, the code will need to be updated to handle platform-specific functionality. To do so, we will use a variation of the conditional build – using well-named files instead of build tags. This is easier to read at the project level and should clearly indicate which files will be compiled for which platform.
Let's create a simple example: we want to read text out loud, but our code only has the ability to do so on macOS (Darwin). We will set up a simple say()
function that does what we want in the say_darwin.go
file:
package main import...