Helix Core is now P4. Product user interfaces will be updated soon. Learn more >>

P4 API for Go Documentation | 2025.1

# Getting Started with P4 API for Go from source code

These steps outline how to set up P4 API for Go on Linux x86_64. For steps on how to get P4 API for C/C++ and the source code for a different operating system, see the following topics:

- Integrate P4 API for Go on Linux

- Integrate P4 API for Go on macOS

- Integrate P4 API for Go on Windows

Follow these steps to set up and run a simple program using P4 API for Go from source.

1. Install Go 1.24 or later. For more information on how to install Go, see Tutorial: Get started with Go.

2. Download P4 API for C/C++ by entering the following code into the terminal.

   Copy

       cd /home/myuser/
       wget http://ftp.perforce.com/perforce/r25.1/bin.linux26x86_64/p4api-glibc2.12-openssl3.tgz
       tar -xvf p4api-glibc2.12-openssl3.tgz

3. Download the source code for P4 API for Go from Github by entering the following code into the terminal.

   Copy

       wget https://github.com/perforce/p4go/archive/refs/tags/v2025.1.2780067.tar.gz
       tar -xvf v2025.1.2780067.tar.gz

4. Using the terminal, create a test directory and create a sample Go code called `main.go`.

   The example below uses Vi to create the sample Go code, but you can use any text editor you prefer.
   Copy

       mkdir p4gotest
       cd p4gotest
       go mod init p4gotest
       vi main.go

5. Enter the following code into `main.go`.

   Copy

       package main

       import "fmt"
       import "p4"

       func main() {
           fmt.Println("P4Go Test program")

           p4 := p4.New()
           defer p4.Close()

           // Set connection details
           p4.SetPort("perforce:1666")
           p4.SetUser("username")
           p4.SetPassword("password")

           // Connect to the P4 server
           connected, err := p4.Connect()
           if err != nil {
               fmt.Println("Error connecting to server:", err)
               return
           }
           if connected {
               fmt.Println("Connected to P4 server.")
           }

           // Run p4 info
           results, err := p4.Run("info")
           if err != nil {
               fmt.Println("Error while running custom command:", err)
               return
           }
           for _, item := range results {
               fmt.Println("Result:", item)
           }

           // Disconnect
           disconnected, err := p4.Disconnect()
           if err != nil {
               fmt.Println("Error disconnecting from server:", err)
           }
           if disconnected {
               fmt.Println("Disconnected from P4 server.")
           }
       }

6. In the terminal, set up build flags with the following code.

   Copy

       go env -w CGO_CPPFLAGS="-I /home/myuser/p4api-2025.1.2761706/include -g"
       go env -w CGO_LDFLAGS="-L /home/myuser/p4api-2025.1.2761706/lib -lp4api -lssl -lcrypto"
       go env

7. In the terminal, run the following code.

   Copy

       go run

Your search for returned results