github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/vet/structtag.go (about)

     1  // Copyright 2010 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This file contains the test for canonical struct tags.
     6  
     7  package main
     8  
     9  import (
    10  	"go/ast"
    11  	"reflect"
    12  	"strconv"
    13  )
    14  
    15  // checkField checks a struct field tag.
    16  func (f *File) checkCanonicalFieldTag(field *ast.Field) {
    17  	if !vet("structtags") {
    18  		return
    19  	}
    20  	if field.Tag == nil {
    21  		return
    22  	}
    23  
    24  	tag, err := strconv.Unquote(field.Tag.Value)
    25  	if err != nil {
    26  		f.Badf(field.Pos(), "unable to read struct tag %s", field.Tag.Value)
    27  		return
    28  	}
    29  
    30  	// Check tag for validity by appending
    31  	// new key:value to end and checking that
    32  	// the tag parsing code can find it.
    33  	if reflect.StructTag(tag+` _gofix:"_magic"`).Get("_gofix") != "_magic" {
    34  		f.Badf(field.Pos(), "struct field tag %s not compatible with reflect.StructTag.Get", field.Tag.Value)
    35  		return
    36  	}
    37  }