Code Monkey home page Code Monkey logo

learninggo's Introduction

Learning Go

File structure

my-go-project/
├── go.mod
├── go.sum
├── cmd/
│   └── myapp/
│       └── main.go
├── pkg/
│   ├── mypackage/
│   │   └── mypackage.go
└── README.md
  1. Folder Structure:
  • project directory, named "my-go-project," contains the following files and directories:
    • go.mod: This file manages your project's dependencies and specifies the Go module name.
    • go.sum: The checksum file for your dependencies.
    • cmd/: A directory where you can place your application-specific commands (e.g., executables).
      • Inside cmd/myapp/, there's a main.go file, which likely contains your application's entry point.
    • pkg/: A directory for your custom packages.
      • Inside pkg/mypackage/, there's a mypackage.go file, which defines your custom package.
  1. Creating a Go Project:
  • To initialize a new Go project (which is essentially a new module), follow these steps:
    • Open your terminal or command prompt.
    • Navigate to the root folder of your project (in your case, the "my-go-project" folder).
    • Run the following command to initialize a Go module:
      go mod init github.com/user/my-go-project
      
  • Replace github.com/user/my-go-project with your desired module path (usually in the format github.com/username/projectname).

Packages and modules in Go:

  1. Packages:

    • A package in Go is a way to organize related code files. It contains a collection of Go source files that provide a set of functions, types, and variables.
    • Packages help modularize your code, making it easier to manage and reuse. You can think of a package as a namespace for related functionality.
    • Commonly used packages (e.g., fmt, os, net/http) are part of the Go standard library and can be imported directly into your code.
  2. Modules:

    • A module in Go is a higher-level unit that encompasses one or more packages. It defines a versioned collection of related packages.
    • When you create a new Go project, you're essentially initializing a new module.
    • Modules are managed using the go.mod file, which specifies the module name, dependencies, and their versions.
    • Modules allow you to track and manage external dependencies more effectively.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.