nopadding

module
v0.0.0-...-ff574a2 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2023 License: BSD-3-Clause

README

nopadding

Provides:

  • A Golang analyzer to show whether your struct has been automatically padded by the Golang compiler
  • A Golang analyzer wrapper which can be invoked from a unit test

This package is very useful during system development (e.g. when marshalling syscall returned structs).

Usage

Put these code segments in your package's unit test.

Check for Non-optimized Struct Layout
package main

import (
	"github.com/jamesits/nopadding/analyzer_wrapper"
	"github.com/stretchr/testify/assert"
	"golang.org/x/tools/go/analysis"
	"golang.org/x/tools/go/analysis/passes/fieldalignment"
	"testing"
)

func TestStructOptimizedLayout(t *testing.T) {
	assert.NotZero(t, analyzer_wrapper.Run(
		[]string{},
		[]*analysis.Analyzer{fieldalignment.Analyzer},
	))
}

Check for Padding Existence
package main

import (
	"github.com/jamesits/nopadding/analyzer_wrapper"
	"github.com/jamesits/nopadding/padding"
	"github.com/stretchr/testify/assert"
	"golang.org/x/tools/go/analysis"
	"testing"
)

func TestStructPadding(t *testing.T) {
	assert.Zero(t, analyzer_wrapper.Run(
		[]string{},
		[]*analysis.Analyzer{padding.Analyzer},
	))
}

Directories

Path Synopsis
Package analyzer_wrapper wraps around Golang's official analysis modules and make them available in a unit test.
Package analyzer_wrapper wraps around Golang's official analysis modules and make them available in a unit test.
Package padding defines an Analyzer that detects structs that is padded automatically by the Golang compiler.
Package padding defines an Analyzer that detects structs that is padded automatically by the Golang compiler.

Jump to

Keyboard shortcuts

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