S
S
sergey_fs2018-03-12 12:56:05
go
sergey_fs, 2018-03-12 12:56:05

How to calculate the last file in a directory?

Can you help with solving the problem of how to indent directory folders
so that it looks like in the
go run main.go example. -f
├───main.go (1881b)
├───main_test.go (1318b)
└───testdata
├───project
│ ├───file.txt (19b)
│ └───gopher. png (
70372b )
_
_
_ js │ └───site.js (10b) ├───zline │ └───empty.txt (empty) └───zzfile.txt (empty) at the moment the output of the program is PS C:\Users\ User\Desktop\GoLang_Projects\src\coursera\hw1> go run .\main.go -f
├───main.go (2238b)
├───main_test.go (1865b)
├───testdata
└───project
├───file.txt (19b)
├───gopher.png (70372b)
└───static
└───a_lorem ├───dolor.txt
(empty)
├───gopher.png (
70372b
) ├───body.css (28b) └───empty.txt (empty) └───html ├───index.html (57b) └───js ├───site.js (10b) └───z_lorem ├───dolor.txt (empty) ├───gopher.png (70372b) ├───ipsum ├───gopher.png (70372b) └───zline ├───empty .txt (empty) ├───lorem ├───dolor.txt (empty)
├───gopher.png (70372b)
├───ipsum
├───gopher.png (70372b)
└───zzfile.txt (empty)
The question is how to indent as in the example above

package main

import (
  "io"
  "os"
  "log"
  "fmt"
  "path/filepath"
  "sort"
  "io/ioutil"
)

var (
  err error

)

func main() {
  out := os.Stdout
  if !(len(os.Args) == 2 || len(os.Args) == 3) {
    panic("usage go run main.go . [-f]")
  }
  path := os.Args[1]
  printFiles := len(os.Args) == 3 && os.Args[2] == "-f"
  err := dirTree(out, path, printFiles)
  if err != nil {
    panic(err.Error())
  }
}

func dirTree(out io.Writer, filePath string, printFiles bool) error   {
  var fileFolder []string  //обьявили массив
///получаю домашнюю директорию пользователя
  home := func(path string, info os.FileInfo, err error) error {
    fileFolder = append(fileFolder,path)
    return nil
  }
  err := filepath.Walk("./", home)
  if err != nil {
    log.Fatal(err)
  }
  sort.Strings(fileFolder) //отсортировали массив
  var sortData []string
  //делаю итерацию по массиву удаляю из массива не нужные файлы
  for k := range fileFolder {
    if fileFolder[k] == "hw1.md" || fileFolder[k] == "dockerfile"  || fileFolder[k] == "./" {
      //log.Println("Err Data is Missing ")
      }else {
      sortData = append(sortData,fileFolder[k])
    }
  }
  //делаем итерацию по отсортированному массиву
  for i := range sortData {
    //getFileSize(sortData[i])
    //element := sortData[i]+getFileSize(sortData[i])
    //выводи данные из массива
    fmt.Println(getTREE(sortData[i])+getFileSize(sortData[i]))
  }
  return  err
}
//Размер файла в домашней директории
func getFileSize(path string) string  {
  var fileSize string
  fileInfo, _ := os.Stat(path)
  if !fileInfo.IsDir() {
    size := fileInfo.Size()
    if size == 0 {
      fileSize = " (empty)"
    } else {
      fileSize = fmt.Sprintf(" (%vb)", size)
    }
  }
  return fileSize
}

func getTREE(path string)  string  {

  var  tabResult string

  files, err := ioutil.ReadDir(filepath.Dir(path))
  if err != nil {
    log.Fatal(err)
  }

  for _, file := range files {
    if file.IsDir() {
      tabResult =   `└───`+filepath.Base(path)
    } else {
      tabResult =  `├───`+filepath.Base(path)
    }
    return  tabResult
  }
  return tabResult
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RidgeA, 2018-03-12
@RidgeA

https://play.golang.com/p/4dv_bvZ7NQz

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question