github.com/goplus/gogen@v1.16.0/chore/goimp/goimp.go (about)

     1  /*
     2   Copyright 2022 The GoPlus Authors (goplus.org)
     3   Licensed under the Apache License, Version 2.0 (the "License");
     4   you may not use this file except in compliance with the License.
     5   You may obtain a copy of the License at
     6       http://www.apache.org/licenses/LICENSE-2.0
     7   Unless required by applicable law or agreed to in writing, software
     8   distributed under the License is distributed on an "AS IS" BASIS,
     9   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10   See the License for the specific language governing permissions and
    11   limitations under the License.
    12  */
    13  
    14  package main
    15  
    16  import (
    17  	"fmt"
    18  	"go/importer"
    19  	"go/token"
    20  	"go/types"
    21  	"os"
    22  	"path/filepath"
    23  	"runtime"
    24  
    25  	"golang.org/x/tools/go/gcexportdata"
    26  )
    27  
    28  func main() {
    29  	val := filepath.Join(runtime.GOROOT(), "pkg/mod")
    30  	os.Setenv("GOMODCACHE", val)
    31  	fmt.Println("GOMODCACHE:", val)
    32  
    33  	var fset = token.NewFileSet()
    34  	var imp types.Importer
    35  	if true {
    36  		imp = importer.ForCompiler(fset, "source", nil)
    37  	} else {
    38  		packages := make(map[string]*types.Package)
    39  		imp = gcexportdata.NewImporter(fset, packages)
    40  	}
    41  
    42  	_, err := imp.Import("go/types")
    43  	fmt.Println("Import result:", err)
    44  
    45  	_, err = imp.Import("golang.org/x/tools/go/gcexportdata")
    46  	fmt.Println("Import result:", err)
    47  }