package src

import "github.com/mmcloughlin/avo/src"

Package src provides types for working with source files.

Index

Examples

Types

type Position

type Position struct {
	Filename string
	Line     int // 1-up
}

Position represents a position in a source file.

func FramePosition

func FramePosition(f runtime.Frame) Position

FramePosition returns the Position of the given stack frame.

func (Position) IsValid

func (p Position) IsValid() bool

IsValid reports whether the position is valid: Line must be positive, but Filename may be empty.

Example

Code:play 

package main

import (
	"fmt"

	"github.com/mmcloughlin/avo/src"
)

func main() {
	fmt.Println(src.Position{"a.go", 42}.IsValid())
	fmt.Println(src.Position{"", 42}.IsValid())
	fmt.Println(src.Position{"a.go", -1}.IsValid())
}

Output:

true
true
false

func (Position) Rel

func (p Position) Rel(basepath string) Position

Rel returns Position relative to basepath. If the given filename cannot be expressed relative to basepath the position will be returned unchanged.

func (Position) Relwd

func (p Position) Relwd() Position

Relwd returns Position relative to the current working directory. Returns p unchanged if the working directory cannot be determined, or the filename cannot be expressed relative to the working directory.

func (Position) String

func (p Position) String() string

String represents Position as a string.

Example

Code:play 

package main

import (
	"fmt"

	"github.com/mmcloughlin/avo/src"
)

func main() {
	fmt.Println(src.Position{"a.go", 42})
	fmt.Println(src.Position{"", 42})
	fmt.Println(src.Position{"a.go", -1}) // invalid
}

Output:

a.go:42
42
-

Source Files

src.go

Version
v0.6.0 (latest)
Published
Jan 7, 2024
Platform
linux/amd64
Imports
4 packages
Last checked
4 weeks ago

Tools for package owners.