go 언어

Yoon·2022년 9월 4일
0

1. GO 설치

Go 프로그래밍을 시작하기 위해 Go 공식 웹사이트인 https://go.dev/dl/ 에서 해당 OS 버젼의 Go를 다운로드하여 설치한다. Go는 Windows, Linux, Mac OS X 에서 사용할 수 있다.

윈도우즈에 Go를 설치하기 위해서는 MSI (*.msi) 파일을 다운받아 실행하면 되는데, Go는 디폴트로 C:\go 폴더에 설치되며, MSI가 C:\go\bin을 PATH 환경변수를 추가한다. (주: 여기서는 별도의 언급이 없는 한 Windows에 설치된 Go를 기준으로 설명)

Go를 설치하고 해당 설치 디렉토리 밑에 bin 디렉토리를 보면 go.exe 파일이 있는데, 이 컴파일러로 go 프로그램을 컴파일하거나 실행할 수 있다. Go 프로그램은 파일 확장자 .go 를 갖는다.

2. GO 실행 테스트(Hello World! 찍기)

Hello World를 시작하세요.

명령 프롬프트를 열고 홈 디렉토리로 이동합니다.
Linux 또는 Mac:

CD
Windows:

CD %홈경로%
첫 번째 Go 소스 코드에 대한 hello 디렉토리를 만듭니다.
예를 들어 다음 명령을 사용합니다.

mkdir 안녕하세요
씨디 안녕하세요
코드에 대한 종속성 추적을 활성화합니다.
코드가 다른 모듈에 포함된 패키지를 가져올 때 코드의 자체 모듈을 통해 이러한 종속성을 관리합니다. 해당 모듈은 해당 패키지를 제공하는 모듈을 추적하는 go.mod 파일에 의해 정의됩니다. 해당 go.mod 파일은 소스 코드 저장소를 포함하여 코드와 함께 유지됩니다.

go.mod 파일을 만들어 코드에 대한 종속성 추적을 활성화하려면 go mod init명령 을 실행하여 코드가 포함될 모듈의 이름을 지정합니다. 이름은 모듈의 모듈 경로입니다.

실제 개발에서 모듈 경로는 일반적으로 소스 코드가 보관되는 저장소 위치입니다. 예를 들어 모듈 경로는 github.com/mymodule. 다른 사람이 사용할 수 있도록 모듈을 게시하려는 경우 모듈 경로 는 Go 도구가 모듈을 다운로드할 수 있는 위치 여야 합니다 . 모듈 경로를 사용하여 모듈 이름을 지정하는 방법에 대한 자세한 내용은 종속성 관리 를 참조하세요 .

이 튜토리얼의 목적상 example/hello.

$ go mod init example/hello
go: 새로운 go.mod 생성: module example/hello
텍스트 편집기에서 코드를 작성할 hello.go 파일을 만듭니다.

다음 코드를 hello.go 파일에 붙여넣고 파일을 저장합니다.

패키지 메인

"fmt" 가져오기

함수 메인() {
fmt.Println("안녕하세요, 세계입니다!")
}
이것은 귀하의 Go 코드입니다. 이 코드에서 다음을 수행합니다.

패키지를 선언하십시오 main(패키지는 기능을 그룹화하는 방법이며 동일한 디렉토리에 있는 모든 파일로 구성됨).
콘솔로의 인쇄를 포함하여 텍스트 서식 지정 기능이 포함 된 인기 있는 fmt패키지 를 가져옵니다. 이 패키지는 Go를 설치할 때 받은 표준 라이브러리 패키지 중 하나입니다.
main콘솔에 메시지를 인쇄 하는 기능을 구현하십시오 . 패키지 main를 실행할 때 기본적으로 함수가 실행됩니다 .main
인사말을 보려면 코드를 실행하세요.

$ 실행 가 .
안녕하세요, 월드입니다!
이 go run명령go 은 Go로 작업을 완료하는 데 사용할 많은 명령 중 하나입니다 . 다음 명령을 사용하여 다른 목록을 가져옵니다.

$ 도움을 받으십시오

Get started with Hello, World.

Open a command prompt and cd to your home directory.
On Linux or Mac:

cd
On Windows:

cd %HOMEPATH%
Create a hello directory for your first Go source code.
For example, use the following commands:

mkdir hello
cd hello
Enable dependency tracking for your code.
When your code imports packages contained in other modules, you manage those dependencies through your code's own module. That module is defined by a go.mod file that tracks the modules that provide those packages. That go.mod file stays with your code, including in your source code repository.

To enable dependency tracking for your code by creating a go.mod file, run the go mod init command, giving it the name of the module your code will be in. The name is the module's module path.

In actual development, the module path will typically be the repository location where your source code will be kept. For example, the module path might be github.com/mymodule. If you plan to publish your module for others to use, the module path must be a location from which Go tools can download your module. For more about naming a module with a module path, see Managing dependencies.

For the purposes of this tutorial, just use example/hello.

$ go mod init example/hello
go: creating new go.mod: module example/hello
In your text editor, create a file hello.go in which to write your code.

Paste the following code into your hello.go file and save the file.

package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}
This is your Go code. In this code, you:

Declare a main package (a package is a way to group functions, and it's made up of all the files in the same directory).
Import the popular fmt package, which contains functions for formatting text, including printing to the console. This package is one of the standard library packages you got when you installed Go.
Implement a main function to print a message to the console. A main function executes by default when you run the main package.
Run your code to see the greeting.

$ go run .
Hello, World!
The go run command is one of many go commands you'll use to get things done with Go. Use the following command to get a list of the others:

$ go help


https://enter.tistory.com/225

내가 직접 한거


  1. 공홈 가서 다운로드 받기
  1. 환경변수 설정해주기
    디폴트는 User\사용자\go 에 있음
    루트는 설치 경로에 따라 다 다름

  1. cmd로 src 가서
    go mod init example/hello 해서 mod 만들기

  1. hello.go 파일 만들기
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
  1. VSCode에서 ctrl + 5 눌러서 콘솔에 Hello World 뜨나보기
    (근데 왜 패키지에 밑줄뜨는지는 모르겠음 ;)
profile
나의 공부 일기

0개의 댓글