Repository: go
Issue: x/tools/cmd/gomvpkg: incorrect handling of cgo packages
Labels: NeedsInvestigation,Tools

### What version of Go are you using (`go version`)?

go version devel +438a757d73 Wed Feb 21 18:10:00 2018 +0000 linux/amd64

### Does this issue reproduce with the latest release?

Yes, on the latest version of `gomvpkg` (rev golang/tools@77106db15f689a60e7d4e085d967ac557b918fb2).

### What operating system and processor architecture are you using (`go env`)?

```
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/u/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/u/Desktop/foo"
GORACE=""
GOROOT="/home/u/go"
GOTMPDIR=""
GOTOOLDIR="/home/u/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build382385690=/tmp/go-build -gno-record-gcc-switches"
```
### What did you do?

Tried to move a package containing cgo code. In this example, we try to move package `a` to `b`.

```bash
gomvpkg -from a -to b
```

Source code of package `a`. Files:
* [a.go](https://play.golang.org/p/ZatrFeaVu8v)

a.go
```go
package a

import "C"

var _ = C.CString("foo")
```

Output source code of package `b`. Files:
* [a.go](https://play.golang.org/p/-dN-RRSgILS)
* [C](https://play.golang.org/p/PO1Z3w7QtdT)

a.go:
```go
// Created by cgo - DO NOT EDIT

//line a.go:1
package b

import _ "unsafe"

var _ = (_Cfunc_CString)("foo")
```

C:
```go
// Created by cgo - DO NOT EDIT

package b

import "unsafe"

import _ "runtime/cgo"

import "syscall"

var _ syscall.Errno

func _Cgo_ptr(ptr unsafe.Pointer) unsafe.Pointer { return ptr }

//go:linkname _Cgo_always_false runtime.cgoAlwaysFalse
var _Cgo_always_false bool

//go:linkname _Cgo_use runtime.cgoUse
func _Cgo_use(interface{})

type _Ctype_char int8

type _Ctype_void [0]byte

//go:linkname _cgo_runtime_cgocall runtime.cgocall
func _cgo_runtime_cgocall(unsafe.Pointer, uintptr) int32

//go:linkname _cgo_runtime_cgocallback runtime.cgocallback
func _cgo_runtime_cgocallback(unsafe.Pointer, unsafe.Pointer, uintptr, uintptr)

//go:linkname _cgoCheckPointer runtime.cgoCheckPointer
func _cgoCheckPointer(interface{}, ...interface{})

//go:linkname _cgoCheckResult runtime.cgoCheckResult
func _cgoCheckResult(interface{})

func _Cfunc_CString(s string) *_Ctype_char {
	p := _cgo_cmalloc(uint64(len(s) + 1))
	pp := (*[1 << 30]byte)(p)
	copy(pp[:], s)
	pp[len(s)] = 0
	return (*_Ctype_char)(p)
}

//go:cgo_import_static _cgo_dbcbf9d0883b_Cfunc__Cmalloc
//go:linkname __cgofn__cgo_dbcbf9d0883b_Cfunc__Cmalloc _cgo_dbcbf9d0883b_Cfunc__Cmalloc
var __cgofn__cgo_dbcbf9d0883b_Cfunc__Cmalloc byte
var _cgo_dbcbf9d0883b_Cfunc__Cmalloc = unsafe.Pointer(&__cgofn__cgo_dbcbf9d0883b_Cfunc__Cmalloc)

//go:linkname runtime_throw runtime.throw
func runtime_throw(string)

//go:cgo_unsafe_args
func _cgo_cmalloc(p0 uint64) (r1 unsafe.Pointer) {
	_cgo_runtime_cgocall(_cgo_dbcbf9d0883b_Cfunc__Cmalloc, uintptr(unsafe.Pointer(&p0)))
	if r1 == nil {
		runtime_throw("runtime: C malloc failed")
	}
	return
}
```

### What did you expect to see?

I expected to see the package moved from `$GOPATH/src/a` to `$GOPATH/src/b`, without affecting cgo code. That is the output should be `b/b.go`:

```go
package b

import "C"

var _ = C.CString("foo")
```

### What did you see instead?

A mysterious `C` file and a rewrite of the code using cgo.

Note, the output (i.e. package `b`) does not compile, while the input did.

```
[u@x220 b]$ go install
# b
a.go:5: undefined: _Cfunc_CString
```