Skip to main content
trimprefix(str, prefix) returns str with the leading prefix removed. If str does not start with prefix, it is returned unchanged.

Signature

trimprefix(str string, prefix string) string

Example

Strip the v prefix from a git tag to get the bare version number for use in artifact names and package manifests:
variable "tag" {
  description = "Git release tag (e.g. v1.4.2)"
  type        = string
}

computed "version" {
  description = "Bare version number without the v prefix"
  expression  = trimprefix(var.tag, "v")
}

task "package" {
  description = "Build release archives with the bare version"
  commands = [
    "go build -ldflags \"-X main.version=${computed.version}\" -o bin/app ./cmd/app",
    "tar -czf myapp-${computed.version}-linux-amd64.tar.gz bin/app",
  ]
}
v1.4.2 becomes 1.4.2.

Notes

  • Only the first occurrence is removed (i.e., if str starts with the prefix exactly once).
  • trimprefix removes a literal string match, not individual characters. Use trim for character-set stripping.