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

P4 API for Go Documentation | 2025.2

# Add and submit files to Perforce

The following is an example of how to add and submit files to Perforce using P4 API for Go.

Copy

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    // Add a file to Perforce
    filePath := "example.txt"
    err := os.WriteFile(filePath, []byte("This is a test file."), 0644)
    if err != nil {
        fmt.Println("Error creating file:", err)
        return
    }
    _, err = p4api.Run("add", filePath)
    if err != nil {
        fmt.Println("Error in adding files:", err)
        return
    }

    // Submit the file
    changeSpec, err := p4api.RunFetch("change")
    if err != nil {
        fmt.Println("Error fetching change spec:", err)
        return
    }
    changeSpec["Description"] = "Adding a new file"
    _, err = p4api.RunSubmit(changeSpec)
    if err != nil {
        fmt.Println("Error submitting change:", err)
    } else {
        fmt.Println("File submitted successfully.")
    }

Your search for returned results