This commit is contained in:
badkaktus 2019-10-11 15:19:29 +03:00
parent f29db8867b
commit c764388d93
2 changed files with 24 additions and 24 deletions

View File

@ -58,7 +58,7 @@ Epoch
Чтение файлов (Reading Files)
Запись файлов (Writing Files)
Строковые фильтры (Line Filters)
File Paths
Пути к файлам (File Paths)
Directories
Temporary Files and Directories
Testing

View File

@ -1,7 +1,7 @@
// The `filepath` package provides functions to parse
// and construct *file paths* in a way that is portable
// between operating systems; `dir/file` on Linux vs.
// `dir\file` on Windows, for example.
// Пакет `filepath` предоставляет функции для разбора и
// создания *путей к файлам* способом, который переносим
// между операционными системами; например, `dir/file` в
// Linux против `dir\file` в Windows.
package main
import (
@ -12,44 +12,44 @@ import (
func main() {
// `Join` should be used to construct paths in a
// portable way. It takes any number of arguments
// and constructs a hierarchical path from them.
// `Join` должен использоваться для создания путей в
// переносимом виде. Он принимает любое количество
// аргументов и строит из них иерархический путь.
p := filepath.Join("dir1", "dir2", "filename")
fmt.Println("p:", p)
// You should always use `Join` instead of
// concatenating `/`s or `\`s manually. In addition
// to providing portability, `Join` will also
// normalize paths by removing superfluous separators
// and directory changes.
// Вы должны всегда использовать `Join` вместо ручного
// объединения с помощью слешей `/` или `\`. В дополнение
// к обеспечению переносимости, `Join` также нормализует
// пути, удаляя лишние разделители.
fmt.Println(filepath.Join("dir1//", "filename"))
fmt.Println(filepath.Join("dir1/../dir1", "filename"))
// `Dir` and `Base` can be used to split a path to the
// directory and the file. Alternatively, `Split` will
// return both in the same call.
// `Dir` и `Base` могут использоваться для разделения
// пути к каталогу и файлу. В качестве альтернативы,
// `Split` вернет оба в одном вызове.
fmt.Println("Dir(p):", filepath.Dir(p))
fmt.Println("Base(p):", filepath.Base(p))
// We can check whether a path is absolute.
// Можно проверить является ли путь абсолютным.
fmt.Println(filepath.IsAbs("dir/file"))
fmt.Println(filepath.IsAbs("/dir/file"))
filename := "config.json"
// Some file names have extensions following a dot. We
// can split the extension out of such names with `Ext`.
// Некоторые имена файлов имеют расширения, следующие
// за точкой. Мы можем получить расширение из таких
// имен с помощью `Ext`.
ext := filepath.Ext(filename)
fmt.Println(ext)
// To find the file's name with the extension removed,
// use `strings.TrimSuffix`.
// Чтобы найти имя файла с удаленным расширением,
// используйте `strings.TrimSuffix`.
fmt.Println(strings.TrimSuffix(filename, ext))
// `Rel` finds a relative path between a *base* and a
// *target*. It returns an error if the target cannot
// be made relative to base.
// `Rel` находит относительный путь между двумя путями
// *base* и *target*. Возвращает ошибку, если *target*
// не может быть получен из *base*.
rel, err := filepath.Rel("a/b", "a/b/t/file")
if err != nil {
panic(err)