gotype

command module
v0.0.0-...-1c8dae9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2019 License: BSD-3-Clause Imports: 16 Imported by: 0

README

Hacked Together GoType

I think this is deprecated now, and you should probably use gopls

What this is

I took gotype from the master branch of Go's git repo and changed a few lines around to make it build in go 1.8.

I added the srcimporter from Go's master branch as well.

I added two flags to it (-origFilename and -modifiedFilename) to enable typechecking as you type in my editor.

I added a plugin for ALE to allow syntax checking in vim.

Why I made this

I used to use go build for my syntax checking needs. It had several issues:

  • I often work in an environment where I cannot build what I'm working on locally due to cgo dependencies. This prevents me from using go build in most packages I work on.

  • I find go build to be extremely resource intensive after doing a git pull or changing branches as it has to rebuild lots of dependencies.

  • Go build only works after a file is saved. So my workflow involved a lot of unnecessary saving to see if my program typechecked.

This hacked up go type solves these problems:

  • It uses the srcimporter rather than trying to compile packages, so packages that depend on C libraries can still be typechecked. Unfortunately, cgo bindings themselves cannot be typechecked.

  • It is faster than go build.

  • I've added arguments to support as-you-type typechecking.

Usage: Vim

  • Download this project.

  • Build gotype and put it somewhere on your PATH:

    go build .
    cp gotype ~/bin/
    
  • Install ALE

  • Put this project's root directory on your vim runtimepath. (I use Plug for this). In your vimrc:

    call plug#begin('~/.vim/plugged')
    ...
    Plug 'w0rp/ale'
    Plug '~/Downloads/gotype/'
    ...
    call plug#end()
    
  • Configure ALE to use gotype on go files. In your vimrc:

    let g:ale_linters = {
    \   'go': ['gotype'],
    \}
    
  • Try it out!

Usage: Emacs

I had a plugin that worked with emacs flycheck, but I've stopped maintaining it. It would be pretty trivial to get it working though.

Caveats

  • Doesn't work directly on cgo code, returns spurious errors.
  • Still fairly slow.

TODO

  • cgo is not correctly handled.
  • Implement checker as a persistant service that can cache unmodified packages. Experiments show a 2x performance improvement.

Documentation

Overview

The gotype command, like the front-end of a Go compiler, parses and type-checks a single Go package. Errors are reported if the analysis fails; otherwise gotype is quiet (unless -v is set).

Without a list of paths, gotype reads from standard input, which must provide a single Go source file defining a complete package.

With a single directory argument, gotype checks the Go files in that directory, comprising a single package. Use -t to include the (in-package) _test.go files. Use -x to type check only external test files.

Otherwise, each path must be the filename of a Go file belonging to the same package.

Imports are processed by importing directly from the source of imported packages (default), or by importing from compiled and installed packages (by setting -c to the respective compiler).

Usage:

gotype [flags] [path...]

The flags are:

-t
	include local test files in a directory (ignored if -x is provided)
-x
	consider only external test files in a directory
-e
	report all errors (not just the first 10)
-v
	verbose mode

Flags controlling additional output:

-ast
	print AST (forces -seq)
-trace
	print parse trace (forces -seq)
-comments
	parse comments (ignored unless -ast or -trace is provided)

Examples:

To check the files a.go, b.go, and c.go:

gotype a.go b.go c.go

To check an entire package including (in-package) tests in the directory dir and print the processed files:

gotype -t -v dir

To verify the output of a pipe:

echo "package foo" | gotype

Package srcimporter implements importing directly from source files rather than installed packages.

Directories

Path Synopsis
Package parser implements a parser for Go source files.
Package parser implements a parser for Go source files.
Package types declares the data types and implements the algorithms for type-checking of Go packages.
Package types declares the data types and implements the algorithms for type-checking of Go packages.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL