Set up P4 API for Go on Windows

Before you install P4 API for Go on Windows, ensure the following:

  • You know which version of Windows you are using (for example, x86 (32-bit), x64 (64-bit), and ARM64)

  • Admin rights (for installations)

To install P4 API for Go on Windows:

Step 1: Get Go 1.24

  1. Go to the Go website and download a version of Go 1.24 that is compatible with your Windows device. For example, for Windows 64-bit, get go1.24.x.Windows-amd64.msi.

  2. Install Go.

  3. (Optional) To verify the installation using the command line, run: go version.

    Expected output: go version go1.24.x Windows/amd64

Step 2: Install MSYS2 (MinGW toolchain)

  1. Go to the MSYS2 website and download a version that matches your Windows system. For example, for Windows 64-bit, get msys2-x86_64-20260322.exe.

  2. Install MSYS2.

  3. Open MSYS2 MinGW64.

  4. In the MinGW64 terminal, install GCC by running:

    pacman -S mingw-w64-x86_64-gcc

  5. (Optional) To verify the installation, in MinGW64 run:

    gcc --version

    If successful, GCC version information is displayed.

Step 3: Make Go available in MINGW64

  1. In the MinGW64 terminal, run:

    export PATH="/c/Program Files/Go/bin:$PATH"

  2. (Optional) To verify Go is available in MINGW64, run:

    go version

    If successful, Go version information is displayed.

Step 4: Download P4 API for C/C++

  1. Go to the Perforce repository.

  2. Navigate to a current release. For example, r25.2.

  3. Download a P4 API for C/C++ build that:

    • Works with MinGW (GCC)

    • Matches OpenSSL 3 (required for compatibility)

    • Is 64-bit

    • Uses POSIX + SEH (correct for modern MINGW64)

    For example, download p4api-openssl3_gcc8_posix_seh.zip.

  4. Extract the contents to a known location. For example, C:\p4api.

  5. Verify that the following files exist:

    • C:\p4api\include\p4\clientapi.h

    • C:\p4api\lib

Step 5: Install OpenSSL (MinGW version)

Do not use a standard Windows OpenSSL installer. It is not compatible with the MinGW toolchain.
  1. In the MinGW64 terminal, run:

    pacman -S mingw-w64-x86_64-openssl

  2. (Optional) To confirm that OpenSSL was installed correctly, check that the required libraries are present:

    1. Run: ls /mingw64/lib | grep ssl

      • Expected output for ssl:

        • libssl.a
        • libssl.dll.a

    2. Run: ls /mingw64/lib | grep crypto

      • Expected output for crypto

        • libcrypto.a

        • libcrypto.dll.a

    If these files are not present, OpenSSL is not installed correctly.

Step 6: Configure CGO, set paths and linker flags

  1. In the MinGW64 terminal, run the following commands:

    1. Enable CGO:

      go env -w CGO_ENABLED=1

    2. Set include path:

      go env -w CGO_CPPFLAGS="-IC:/p4api/include -DOS_NT -g"

    3. Set linker flags:

      go env -w CGO_LDFLAGS="-LC:/p4api/lib -L/mingw64/lib -lp4api -lssl -lcrypto -lcrypt32 -lws2_32 -lole32 -lshell32 -luser32 -ladvapi32 -pthread -v"

  2. (Optional) To verify the configuration is in place, run:

    go env CGO_LDFLAGS

    Check the output matches the configuration entered above.

Step 7: Create a test project

  1. Create a folder in a known location. For example, c:\p4test.

  2. In the new folder, create a file named main.go and enter the following code.

    Copy
    package main

    import (
        "fmt"
        p4go "github.com/perforce/p4go"
    )

    func main() {
        p4conn := p4go.New()

        _, err := p4conn.Connect()
        if err != nil {
            fmt.Println("Connection failed:", err)
            return
        }

        fmt.Println("Connected!")

        p4conn.Disconnect()
    }

Step 8: Build the project

  1. In the MinGW64 terminal, to navigate to the test folder, run:

    cd /c/p4test

  2. Initialize the module:

    go mod init p4test

  3. Download the dependencies:

    go mod tidy

  4. (Optional) Check the test folder to confirm that the following files are present.

    • main.go

    • go.mod

    • go.sum

    If they are, the download and setup completed successfully.

  5. Build the project:

    go build -x

    The build completes with no errors, you will see the this file:

    p4test.exe

Step 9: Run the program

  1. In the MinGW64 terminal, run the program:

    ./p4test.exe

    If successfully installed, you will see either:

    • Connected!

    • Connection failed: <message>

    Both indicate that P4 API for Go is installed and working correctly.

Next steps

View the documentation forP4 API for Go structs and interfaces to see what other commands are available when using P4 API for Go.