去'mod init'创建新文件夹?路径的意义是什么?

时间:2020-01-13 14:25:44

标签: go go-modules

只有3天的Go语言经验。希望有一个例子能更容易理解我的困惑。

<Page
x:Class="Virtualization.Scenario5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Virtualization"
xmlns:ba="using:Windows.Storage.BulkAccess"    
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="48"/>
    </Grid.ColumnDefinitions>
    <StackPanel Grid.ColumnSpan="1" Orientation="Horizontal"
                BorderBrush="Blue" BorderThickness="0,0,2,2">
        <AppBarButton Icon="Folder"
                      LabelPosition="Collapsed"
                      Click="FolderPickerButton_Click"/>
        <AppBarButton Icon="Rename"
                      LabelPosition="Collapsed"
                      Click="RenameButton_Click"/>
        <AppBarButton Icon="Clear"
                      LabelPosition="Collapsed" Label="Select Folder"
                      Click="ClearButton_Click"/>
    </StackPanel>
    <ListView x:Name="itemCollectionGridView" Grid.Row="1" Grid.Column="1"
              ItemsSource="{x:Bind ItemCollectionView, Mode=OneWay}" IsItemClickEnabled="True"
              ItemClick="ListView_ItemClick">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="ba:FileInformation">
                <StackPanel MinHeight="100">
                    <TextBlock Text="{Binding Name}" TextWrapping="WrapWholeWords"/>
                    <TextBlock Text="{Binding Path}" TextWrapping="WrapWholeWords"/>
                    <TextBlock Text="{Binding FolderRelativeId}" TextWrapping="WrapWholeWords"/>

                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

    <ListView Grid.Row="1" Grid.Column="2" ItemsSource="{x:Bind Information, Mode=OneWay}">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <ItemsStackPanel ItemsUpdatingScrollMode="KeepLastItemInView" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

</Grid>

在上面的示例中,root@freebsd1:/usr/home/arun/go-start/src/test2 # go mod init f1/f2/f3/f4/f5/hello go: creating new go.mod: module f1/f2/f3/f4/f5/hello root@freebsd1:/usr/home/arun/go-start/src/test2 # 正在创建所有这些文件夹(go mod init)?我搜索了很多东西,在系统中找不到任何这样的文件夹。 那这条路的意义是什么?

即使未提及此路径,即使下面的命令也不会运行

f1/f2/f3/f4/f5/hello

-:编辑:-

这可能会在以后对某人有所帮助(只是逐步按照正确的方式来理解这一点,尤其是对于新手而言)

  1. 我正计划创建一个程序“ calculator”,并将稍后在GitHub中上传。

  2. 我会将功能保留在# go install f1/f2/f3/f4/f5/hello sum等不同的包中

  3. 第一步multiply(在这里不要混淆,这只是一个假设,将来我可能会在github中为此项目创建一个存储库)

  4. 创建了文件夹#go mod init github.com/go-arun/calculator(包文件夹之一,并在内部创建了sum

查看按系统分类的信息:

1。

sum.go

2。

root@debian1:/home/arun/lab# go mod init github.com/go-arun/calculator
go: creating new go.mod: module github.com/go-arun/calculator

root@debian1:/home/arun/lab# cat go.mod
module github.com/go-arun/calculator

go 1.15

3。

root@debian1:/home/arun/lab# cat sum/sum.go
package sum

import "fmt"

func Sum(num1,num2 int)(){
        fmt.Println(num1+num2)

}

4。

root@debian1:/home/arun/lab# cat main.go
package main

import(
        "github.com/go-arun/calculator/sum"
)

func main(){
        n1 := 10
        n2 := 10

        sum.Sum(n1,n2)
}

1 个答案:

答案 0 :(得分:5)

go mod init不会创建这些文件夹。您将“模块路径”传递给go mod init,该路径记录在它创建的go.mod文件中。

“模块路径”是对应于模块根目录的导入路径前缀。模块路径和模块根目录的相对路径一起形成完整的导入路径,该路径在应用程序中必须唯一。

因此,例如,如果您的模块包含一个名为foo的文件夹(其中包含一个软件包foo),则会通过路径modulepath/foo导入它。您的情况应该是f1/f2/f3/f4/f5/hello/foo

允许moduleA包含一个foo包,并且允许moduleB包含一个foo包。使用/导入时,第一个将像moduleA/foo一样被导入,而第二个像moduleB/foo一样被导入,因此您要导入的是明确的。模块路径就像一个名称空间。

建议使用与您计划或要向其发布模块的存储库相对应的模块路径,这样,go get将能够自动获取,构建和安装模块。例如,您可以选择一个模块路径github.com/bob/hello,因此在发布模块时,每个人都可以在其应用程序中使用import "github.com/bob/hello"来获取它。

还请注意,您无需先将代码发布到远程存储库即可进行构建。但是仍然建议您遵循这种模式,这样,如果您决定发布它,那么将来要做的工作会更少。这里没什么可失去的。

文档中的更多信息:Command go: Defining a module

也:How to Write Go Code: Code organization

相关问题