Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
go - copy relative folder using embed

I am using our go library (it could be changed if necessary) with a folder named "devops", which contains nested content.

in the library, I copy the devops folder with his content, to another folder.

this is the copy function:

func CopyDirectory(scrDir, dest string) error { // scrDir = devops
entries, err := ioutil.ReadDir(scrDir)
if err != nil {
    return err
}
for _, entry := range entries {
    sourcePath := filepath.Join(scrDir, entry.Name())
    destPath := filepath.Join(dest, entry.Name())

    fileInfo, err := os.Stat(sourcePath)
    fmt.Println(fileInfo)
    if err != nil {
        return err
    }

    stat, ok := fileInfo.Sys().(*syscall.Stat_t)
    if !ok {
        return fmt.Errorf("failed to get raw syscall.Stat_t data for '%s'", sourcePath)
    }

    switch fileInfo.Mode() & os.ModeType {
    case os.ModeDir:
        if err := CreateIfNotExists(destPath, 0755); err != nil { // CreateIfNotExists is a internal function
            return err
        }
        if err := CopyDirectory(sourcePath, destPath); err != nil { // CopyDirectory is a internal function
            return err
        }
    case os.ModeSymlink:
        if err := CopySymLink(sourcePath, destPath); err != nil { // CopySymLink is a internal function
            return err
        }
    default:
        if err := Copy(sourcePath, destPath); err != nil { // Copy is a internal function
            return err
        }
    }

    if err := os.Lchown(destPath, int(stat.Uid), int(stat.Gid)); err != nil {
        return err
    }

    isSymlink := entry.Mode()&os.ModeSymlink != 0
    if !isSymlink {
        if err := os.Chmod(destPath, entry.Mode()); err != nil {
            return err
        }
    }
}
return nil

}

the library runs well as self-running, but when I use it from another project, it tries to copy the devops folder from the project path, and of course, fails.

I tried doing it with embed pkg, but the code fails when calling

fileInfo.Sys().(*syscall.Stat_t)

with an error that doesn't give any details.

this is the my code:

//go:embed devops
var f embed.FS
...

dir, _ := f.ReadDir("devops")

for _, entry := range dir { 
    fileInfo, _ := entry.Info()

    err, ok := fileInfo.Sys().(*syscall.Stat_t)
    if !ok {
        println(err) // prints 0x0
    }
}

How to solve it?

P.S. the code runs in Linux OS.

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda