Quick start for P4 API for Go on Windows
On this page:
Before you begin
Ensure you have the following:
-
Go version 1.24 or above. Download from the Go website.
-
MSYS2 (MinGW64 environment). Required to provide the GCC toolchain used by P4 API for Go. Download from the MSYS2 website..
-
A version of P4 API for C/C++ compatible with MinGW. Download from the Perforce repository.
-
Access to a working version of P4 Server.
-
The server and port values for P4 Server.
-
The username for your P4 user.
-
The name of the workspace that contains the files you want to check out, edit, and commit.
-
(Optional) A working version of P4V, to check if a file has been added to a changelist.
Install P4 API for Go
P4 API for Go is built from source and requires a MinGW64 environment. The steps below provide a high-level overview of the installation process:
-
Install Go.
-
Install MSYS2 and toolchain.
-
Make Go available in MinGW64.
-
Install P4 API for C/C++.
-
Configure CGO.
-
Create and build a test app.
-
Run the test app and check output.
For the full installation steps, see Set up P4 API for Go on Windows.
Connect the API to P4 Server
Use the following script to connect to P4 Server and view the first 10 files in a given workspace.
Update the following credentials in the script:
-
P4PORT- Add the server and port to connect to in the format:server:port. This is usually,localhost:1666. -
P4USER- A username for your P4 user. To find a list of users, runp4 usersin the command line. -
P4CLIENT- The name of the workspace you want to connect to. To find a list of available workspaces, runp4 clientsin the command line.
Call the script something simple like connect_to_p4.go.
package main
import (
"fmt"
"os"
p4go "github.com/perforce/p4go"
)
func main() {
// --- Set environment variables ---
os.Setenv("P4PORT", "localhost:1666") // server:port
os.Setenv("P4USER", "P4UserName") // username
os.Setenv("P4CLIENT", "go_workspace") // workspace name
// Create connection
p4conn := p4go.New()
// Connect to the server
_, err := p4conn.Connect()
if err != nil {
fmt.Println("Connection failed:", err)
return
}
fmt.Println("Connected!")
// Get server info
info, err := p4conn.Run("info")
if err == nil && len(info) > 0 {
if server, ok := info[0]["serverAddress"]; ok {
fmt.Println("Connected to:", server)
}
}
// Get up to 10 files in workspace
files, err := p4conn.Run("have")
if err != nil {
fmt.Println("Failed to list files:", err)
} else {
for i, f := range files {
if i >= 10 {
break
}
fmt.Printf(" - %s #%s\n", f["depotFile"], f["rev"])
}
}
// Disconnect
p4conn.Disconnect()
fmt.Println("Disconnected from the P4 Server.")
}
To run the script:
-
Open a MinGW64 terminal and navigate to the folder containing the script.
You must runconnect_to_p4.goin the MSYS2 MinGW64 terminal, as the required GCC toolchain and OpenSSL libraries are only available in that environment. -
Run
go run connect_to_p4.go(If you have given the script a different name, use that name).You may see a lot of output during the build. This is normal and does not mean anything is wrong. -
The list of files is displayed as structured data. For example:
map[depotFile://DemoDepot/... rev:1 ...]
You should see up to 10 files listed in your chosen workspace.
If you receive any errors when connecting to the P4 Server, see Troubleshooting.
Check out a file
The following script will:
-
Connect to P4 Server with your credentials (user and workspace).
-
Check out a file and add it to the default changelist.
-
Return a brief summary of what has been done.
-
Disconnect from the P4 Server cleanly.
Update the following credentials in the script:
-
P4PORT- Add the server and port to connect to in the format:server:port -
P4USER- A username for your P4 user. -
P4CLIENT- The name of the workspace you want to connect to. -
DEPOT_FILE- Filepath to the file to edit. For example:C:/workspaces/DemoDepot/go/Ascii_art/README.md-
Use Windows-style paths (for example, C:/workspace/file.txt). Do not use MinGW-style paths such as /c/workspace/file.txt.
-
Call the script something simple like check_out_file.go.
package main
import (
"fmt"
"os"
p4go "github.com/perforce/p4go"
)
func main() {
// --- Set connection details ---
os.Setenv("P4PORT", "localhost:1666") // server:port
os.Setenv("P4USER", "P4UserName") // username
os.Setenv("P4CLIENT", "go_workspace") // workspace name
// File to check out
file := "C:/workspaces/DemoDepot/go/Ascii_art/README.md"
// Create connection
p4conn := p4go.New()
// Connect
_, err := p4conn.Connect()
if err != nil {
fmt.Println("Connection failed:", err)
return
}
fmt.Println("Connected!")
// Check out (open for edit)
result, err := p4conn.Run("edit", file)
if err != nil {
fmt.Println("Checkout failed:", err)
} else {
fmt.Println("Checkout result:")
for _, r := range result {
fmt.Println(r)
}
}
// Disconnect
p4conn.Disconnect()
fmt.Println("Disconnected from the P4 Server.")
}
To run the script:
-
Open a MinGW64 terminal and navigate to the folder containing the script.
-
Run
go run check_out_file.go(If you have given the script a different name, use that name).You may see a lot of output during the build. This is normal and does not mean anything is wrong.
Your chosen file is now available to edit.
If you have P4V, you will see the file added to the default changelist.
Commit a file
The following script will:
-
Connect to the P4 Server
-
Submit the default changelist with a description and print the result.
-
Disconnect from P4 Server.
Update the following credentials in the script:
-
P4PORT- Add the server and port to connect to in the format:server:port -
P4USER- A username for your P4 user. -
P4CLIENT- The name of the workspace you want to connect to.
Call the script something simple like commit_file.go,
package main
import (
"fmt"
"os"
p4go "github.com/perforce/p4go"
)
func main() {
// --- Set connection details ---
os.Setenv("P4PORT", "localhost:1666") // server:port
os.Setenv("P4USER", "P4UserName") // username
os.Setenv("P4CLIENT", "go_workspace") // workspace name
// Create connection
p4conn := p4go.New()
// Connect
_, err := p4conn.Connect()
if err != nil {
fmt.Println("Connection failed:", err)
return
}
fmt.Println("Connected!")
// Submit default changelist
result, err := p4conn.Run("submit", "-d", "Submitted from P4Go")
if err != nil {
fmt.Println("Submit failed:", err)
} else {
fmt.Println("Submit result:")
for _, r := range result {
fmt.Println(r)
}
}
// Disconnect
p4conn.Disconnect()
fmt.Println("Disconnected from the P4 Server.")
}
To run the script:
-
Open a MinGW64 terminal and navigate to the folder containing the script.
-
Run
go run commit_file.go.Again, you may see a lot of output during the build. This is normal and does not mean anything is wrong.
Your chosen file has now been committed. If you check P4V, you will no longer be able to see the file in the default changelist.
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.