пути
This commit is contained in:
parent
f29db8867b
commit
c764388d93
@ -58,7 +58,7 @@ Epoch
|
|||||||
Чтение файлов (Reading Files)
|
Чтение файлов (Reading Files)
|
||||||
Запись файлов (Writing Files)
|
Запись файлов (Writing Files)
|
||||||
Строковые фильтры (Line Filters)
|
Строковые фильтры (Line Filters)
|
||||||
File Paths
|
Пути к файлам (File Paths)
|
||||||
Directories
|
Directories
|
||||||
Temporary Files and Directories
|
Temporary Files and Directories
|
||||||
Testing
|
Testing
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
// The `filepath` package provides functions to parse
|
// Пакет `filepath` предоставляет функции для разбора и
|
||||||
// and construct *file paths* in a way that is portable
|
// создания *путей к файлам* способом, который переносим
|
||||||
// between operating systems; `dir/file` on Linux vs.
|
// между операционными системами; например, `dir/file` в
|
||||||
// `dir\file` on Windows, for example.
|
// Linux против `dir\file` в Windows.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -12,44 +12,44 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// `Join` should be used to construct paths in a
|
// `Join` должен использоваться для создания путей в
|
||||||
// portable way. It takes any number of arguments
|
// переносимом виде. Он принимает любое количество
|
||||||
// and constructs a hierarchical path from them.
|
// аргументов и строит из них иерархический путь.
|
||||||
p := filepath.Join("dir1", "dir2", "filename")
|
p := filepath.Join("dir1", "dir2", "filename")
|
||||||
fmt.Println("p:", p)
|
fmt.Println("p:", p)
|
||||||
|
|
||||||
// You should always use `Join` instead of
|
// Вы должны всегда использовать `Join` вместо ручного
|
||||||
// concatenating `/`s or `\`s manually. In addition
|
// объединения с помощью слешей `/` или `\`. В дополнение
|
||||||
// to providing portability, `Join` will also
|
// к обеспечению переносимости, `Join` также нормализует
|
||||||
// normalize paths by removing superfluous separators
|
// пути, удаляя лишние разделители.
|
||||||
// and directory changes.
|
|
||||||
fmt.Println(filepath.Join("dir1//", "filename"))
|
fmt.Println(filepath.Join("dir1//", "filename"))
|
||||||
fmt.Println(filepath.Join("dir1/../dir1", "filename"))
|
fmt.Println(filepath.Join("dir1/../dir1", "filename"))
|
||||||
|
|
||||||
// `Dir` and `Base` can be used to split a path to the
|
// `Dir` и `Base` могут использоваться для разделения
|
||||||
// directory and the file. Alternatively, `Split` will
|
// пути к каталогу и файлу. В качестве альтернативы,
|
||||||
// return both in the same call.
|
// `Split` вернет оба в одном вызове.
|
||||||
fmt.Println("Dir(p):", filepath.Dir(p))
|
fmt.Println("Dir(p):", filepath.Dir(p))
|
||||||
fmt.Println("Base(p):", filepath.Base(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"))
|
||||||
fmt.Println(filepath.IsAbs("/dir/file"))
|
fmt.Println(filepath.IsAbs("/dir/file"))
|
||||||
|
|
||||||
filename := "config.json"
|
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)
|
ext := filepath.Ext(filename)
|
||||||
fmt.Println(ext)
|
fmt.Println(ext)
|
||||||
|
|
||||||
// To find the file's name with the extension removed,
|
// Чтобы найти имя файла с удаленным расширением,
|
||||||
// use `strings.TrimSuffix`.
|
// используйте `strings.TrimSuffix`.
|
||||||
fmt.Println(strings.TrimSuffix(filename, ext))
|
fmt.Println(strings.TrimSuffix(filename, ext))
|
||||||
|
|
||||||
// `Rel` finds a relative path between a *base* and a
|
// `Rel` находит относительный путь между двумя путями
|
||||||
// *target*. It returns an error if the target cannot
|
// *base* и *target*. Возвращает ошибку, если *target*
|
||||||
// be made relative to base.
|
// не может быть получен из *base*.
|
||||||
rel, err := filepath.Rel("a/b", "a/b/t/file")
|
rel, err := filepath.Rel("a/b", "a/b/t/file")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user