def group_by_go_package(proto_files):
    """Groups the file paths by `option go_package` in the file.
    This reflects the logic in https://github.com/google/go-genproto/blob/master/regen.go

    Returns:
        A dict mapping go_package to the list of proto files in the package.
    """

    def go_pkg(file):
        prefix = 'option go_package ='
        with open(file, encoding='utf-8') as f:
            for line in f:
                if line.startswith(prefix):
                    # syntax is: option go_package = "path/to/package;nickname";
                    # the nickname is optional
                    line = line[len(prefix):]
                    line = line.strip().strip('";')
                    # find the nickname
                    semi = line.find(';')
                    if semi >= 0:
                        line = line[:semi]
                    return line
        return ""

    pkgs = {}
    for file in proto_files:
        pkg = go_pkg(file)
        pkgs.setdefault(pkg, []).append(file)

    return pkgs