'Programming'에 해당되는 글 1785건

  1. 2023.05.16 golang switch, select
  2. 2023.05.16 golang uds
  3. 2023.05.16 golang mutex (sync)
  4. 2023.05.11 go 포맷터
  5. 2023.05.11 c에서 rust 호출하기
  6. 2023.05.11 rust 실행파일
  7. 2023.05.11 rust if문
  8. 2023.05.10 rust rustup doc
  9. 2023.05.09 rust cargo new를 통한 프로젝트 생성
  10. 2023.05.09 rust 와 main.rs
Programming/golang2023. 5. 16. 11:32

golang switch는 신형 언어에 확장되서 그런가 꽤나 만능이다.

특이하게 조건식도 가능하고, 케이스 리스트도 된다.(c#에서 얼핏 봤던 느낌..)

package main

import (
"fmt"
"time"
)

func main() {
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("Good morning!")
case t.Hour() < 17:
fmt.Println("Good afternoon.")
default:
fmt.Println("Good evening.")
}
}

[링크 :https://go.dev/tour/flowcontrol/11]

 

func WhiteSpace(c rune) bool {
switch c {
case ' ', '\t', '\n', '\f', '\r':
return true
}
return false
}

[링크 : https://hamait.tistory.com/1017]

 

아무튼 select는 channel 처리에 좀더 특화된 구문으로 생긴것 자체는 switch - case와 동일하게 작성된다.

다만, 동시에 여러개가 들어왔을 경우 랜덤하게 실행된다고 한다.

(생각이 꼬였는지 동시에 들어오면 가장 위에꺼 부터 실행되어야 하는거 아냐? 싶은데 동시성이니까 랜덤하게 처리되는건가..)

The select statement lets a goroutine wait on multiple communication operations.

A select blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.

 

package main

import "fmt"

func fibonacci(c, quit chan int) {
x, y := 0, 1
for {
select {
case c <- x:
x, y = y, x+y
case <-quit:
fmt.Println("quit")
return
}
}
}

func main() {
c := make(chan int)
quit := make(chan int)
go func() {
for i := 0; i < 10; i++ {
fmt.Println(<-c)
}
quit <- 0
}()
fibonacci(c, quit)
}

[링크 : https://go.dev/tour/concurrency/5]

 

[링크 : https://edu.goorm.io/learn/lecture/2010/한-눈에-끝내는-고랭-기초/lesson/382961/채널-select문]

 

'Programming > golang' 카테고리의 다른 글

golang 동시성  (0) 2023.05.24
golang 고루틴과 채널  (0) 2023.05.16
golang uds  (0) 2023.05.16
golang mutex (sync)  (0) 2023.05.16
go 포맷터  (0) 2023.05.11
Posted by 구차니
Programming/golang2023. 5. 16. 10:46

쓸만하려나?

server client
package main

import (
"fmt"
"log"
"os"
"path/filepath"
"time"

"github.com/google/uuid"
"github.com/johnsiilver/golib/ipc/uds"
)

func main() {
socketAddr := filepath.Join(os.TempDir(), uuid.New().String())

cred, _, err := uds.Current()
if err != nil {
panic(err)
}

// This will set the socket file to have a uid and gid of whatever the
// current user is. 0770 will be set for the file permissions (though on some
// systems the sticky bit gets set, resulting in 1770.
serv, err := uds.NewServer(socketAddr, cred.UID.Int(), cred.GID.Int(), 0770)
if err != nil {
panic(err)
}

fmt.Println("Listening on socket: ", socketAddr)

// This listens for a client connecting and returns the connection object.
for conn := range serv.Conn() {
conn := conn

// We spinoff handling of this connection to its own goroutine and
// go back to listening for another connection.
go func() {
// We are checking the client's user ID to make sure its the same
// user ID or we reject it. Cred objects give you the user's
// uid/gid/pid for filtering.
if conn.Cred.UID.Int() != cred.UID.Int() {
log.Printf("unauthorized user uid %d attempted a connection", conn.Cred.UID.Int())
conn.Close()
return
}
// Write to the stream every 10 seconds until the connection closes.
for {
if _, err := conn.Write([]byte(fmt.Sprintf("%s\n", time.Now().UTC()))); err != nil {
conn.Close()
}
time.Sleep(10 * time.Second)
}
}()
}
}
package main

import (
"flag"
"fmt"
"io"
"os"

"github.com/johnsiilver/golib/ipc/uds"
)

var (
addr = flag.String("addr", "", "The path to the unix socket to dial")
)

func main() {
flag.Parse()

if *addr == "" {
fmt.Println("did not pass --addr")
os.Exit(1)
}

cred, _, err := uds.Current()
if err != nil {
panic(err)
}

// Connects to the server at socketAddr that must have the file uid/gid of
// our current user and one of the os.FileMode specified.
client, err := uds.NewClient(*addr, cred.UID.Int(), cred.GID.Int(), []os.FileMode{0770, 1770})
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// client implements io.ReadWriteCloser and this will print to the screen
// whatever the server sends until the connection is closed.
io.Copy(os.Stdout, client)
}

[링크 : https://github.com/johnsiilver/golib/blob/v1.1.2/ipc/uds/example/server/server.go]

[링크 : https://github.com/johnsiilver/golib/blob/v1.1.2/ipc/uds/example/client/client.go]

[링크 : https://pkg.go.dev/github.com/johnsiilver/golib/ipc/uds]

'Programming > golang' 카테고리의 다른 글

golang 고루틴과 채널  (0) 2023.05.16
golang switch, select  (0) 2023.05.16
golang mutex (sync)  (0) 2023.05.16
go 포맷터  (0) 2023.05.11
golang echo directory listing  (0) 2023.05.08
Posted by 구차니
Programming/golang2023. 5. 16. 10:36

 

package main

import (
"fmt"
"sync"
"time"
)

// SafeCounter is safe to use concurrently.
type SafeCounter struct {
mu sync.Mutex
v  map[string]int
}

// Inc increments the counter for the given key.
func (c *SafeCounter) Inc(key string) {
c.mu.Lock()
// Lock so only one goroutine at a time can access the map c.v.
c.v[key]++
c.mu.Unlock()
}

// Value returns the current value of the counter for the given key.
func (c *SafeCounter) Value(key string) int {
c.mu.Lock()
// Lock so only one goroutine at a time can access the map c.v.
defer c.mu.Unlock()
return c.v[key]
}

func main() {
c := SafeCounter{v: make(map[string]int)}
for i := 0; i < 1000; i++ {
go c.Inc("somekey")
}

time.Sleep(time.Second)
fmt.Println(c.Value("somekey"))
}

[링크 : https://go.dev/tour/concurrency/9]

 

func (*Mutex) Lock ¶
func (m *Mutex) Lock()
Lock locks m. If the lock is already in use, the calling goroutine blocks until the mutex is available.

func (*Mutex) TryLock ¶
added in go1.18
func (m *Mutex) TryLock() bool
TryLock tries to lock m and reports whether it succeeded.

Note that while correct uses of TryLock do exist, they are rare, and use of TryLock is often a sign of a deeper problem in a particular use of mutexes.

func (*Mutex) Unlock ¶
func (m *Mutex) Unlock()
Unlock unlocks m. It is a run-time error if m is not locked on entry to Unlock.

A locked Mutex is not associated with a particular goroutine. It is allowed for one goroutine to lock a Mutex and then arrange for another goroutine to unlock it.

[링크 : https://pkg.go.dev/sync#Mutex.Lock]

 

[링크 : https://mingrammer.com/gobyexample/mutexes/]

[링크 : https://www.joinc.co.kr/w/GoLang/example/mutexex]

[링크 : https://pyrasis.com/book/GoForTheReallyImpatient/Unit35]

 

'Programming > golang' 카테고리의 다른 글

golang switch, select  (0) 2023.05.16
golang uds  (0) 2023.05.16
go 포맷터  (0) 2023.05.11
golang echo directory listing  (0) 2023.05.08
golang websocket binary  (0) 2023.03.28
Posted by 구차니
Programming/golang2023. 5. 11. 11:56

go 에는 자체적으로 포맷터가 들어있다.

획일화 되서 나쁘다고 해야하나.. 좋다고 해야하나.. 참 미묘~

 

To format your code, you can use the gofmt tool directly:

gofmt -w yourcode.go
Or you can use the “go fmt” command:

go fmt path/to/your/package

[링크 : https://go.dev/blog/gofmt]

'Programming > golang' 카테고리의 다른 글

golang uds  (0) 2023.05.16
golang mutex (sync)  (0) 2023.05.16
golang echo directory listing  (0) 2023.05.08
golang websocket binary  (0) 2023.03.28
golang 크로스 컴파일 GOARM GOARCH  (0) 2023.02.03
Posted by 구차니
Programming/rust2023. 5. 11. 10:16

no_mangle을 설정하면 c에서 사용가능한 함수로 빌드 되는 듯.

Every function in your Rust-ffi API needs to have a corresponding header function.

#[no_mangle]
pub extern "C" fn rust_function() {}

would then become
void rust_function();

[링크 : https://docs.rust-embedded.org/book/interoperability/rust-with-c.html]

[링크 : https://dev.to/dandyvica/how-to-call-rust-functions-from-c-on-linux-h37]

 

mangle.. mangle이면 cpp이랑 좀 더 용이하게 붙을 느낌인데..?

$ readelf -a main | grep demangle
   387: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS rustc_demangle.9c38528e-c
   393: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS rustc_demangle.9c38528e-c
   394: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS rustc_demangle.9c38528e-c
   395: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS rustc_demangle.9c38528e-c
   400: 000000000002fc20   320 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v010H
   401: 000000000002fd60   152 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v06Pa
   402: 000000000002fe00   181 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v06Pa
   403: 000000000002fec0   202 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v06Pa
   404: 000000000002ff90    85 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v06Pa
   405: 000000000002fff0   471 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v06Pa
   406: 00000000000301d0    79 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   407: 0000000000030220   411 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   408: 00000000000303c0   411 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   409: 0000000000032a00  1868 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   410: 0000000000030560   396 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   411: 0000000000031c00  1282 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   412: 00000000000306f0   354 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   413: 0000000000030860   241 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   414: 0000000000030960   748 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   415: 0000000000032800   509 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   416: 0000000000030c50   514 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   417: 0000000000032110  1138 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   418: 0000000000030e60   152 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   419: 0000000000030f00   159 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   420: 0000000000030fa0   666 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   421: 0000000000031240   147 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   422: 0000000000031af0   260 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   423: 00000000000312e0   164 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   424: 0000000000032590   610 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   425: 0000000000033150   487 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   426: 0000000000033340   720 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   427: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS rustc_demangle.9c38528e-c
   429: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS rustc_demangle.9c38528e-c
   430: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS rustc_demangle.9c38528e-c
   492: 000000000002f460   635 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v08de
   624: 0000000000033620   802 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle6legac
   654: 0000000000031390  1878 FUNC    LOCAL  DEFAULT   14 _ZN14rustc_demangle2v07Pr
   758: 000000000002ea40   395 FUNC    GLOBAL DEFAULT   14 _ZN63_$LT$rustc_demangle.
   848: 000000000002e330  1695 FUNC    GLOBAL DEFAULT   14 _ZN14rustc_demangle8deman
   956: 0000000000033950  2970 FUNC    GLOBAL DEFAULT   14 _ZN71_$LT$rustc_demangle.
  1021: 000000000002f6e0  1332 FUNC    GLOBAL DEFAULT   14 _ZN64_$LT$rustc_demangle.
  1090: 000000000002ea30     9 FUNC    GLOBAL DEFAULT   14 _ZN14rustc_demangle8Deman
  1278: 000000000002e9d0    82 FUNC    GLOBAL DEFAULT   14 _ZN14rustc_demangle12try_
  1303: 000000000002ebd0    21 FUNC    GLOBAL DEFAULT   14 _ZN71_$LT$rustc_demangle.

'Programming > rust' 카테고리의 다른 글

rust 소유권  (0) 2023.05.25
rust was  (0) 2023.05.20
rust 실행파일  (0) 2023.05.11
rust if문  (0) 2023.05.11
rust rustup doc  (0) 2023.05.10
Posted by 구차니
Programming/rust2023. 5. 11. 10:10

dynamic link 이고

$ file *
main:    ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=989d3d9b6419e9e16be59fe589ddda8c631c30f0, with debug_info, not stripped
main.rs: C source, ASCII text

 

링크 된걸 보면 c 프로그램과 별 차이가 없긴 한데

$ ldd main
linux-vdso.so.1 (0x00007ffe605de000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007efd04cd8000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007efd04ad0000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007efd048b1000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007efd046ad000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007efd042bc000)
/lib64/ld-linux-x86-64.so.2 (0x00007efd05142000)

 

실행 파일 크기가 미친듯이 크다.

golang 처럼 rust 라이브러리 자체는 static으로 link 한건가?

$ ls -alh
합계 12M
drwxrwxr-x  2 user user 4.0K  5월 11 10:06 .
drwxrwxr-x 24 user user 4.0K  5월 11 10:05 ..
-rwxrwxr-x  1 user user  12M  5월 11 10:06 main
-rw-rw-r--  1 user user  148  5월 11 10:06 main.rs

'Programming > rust' 카테고리의 다른 글

rust was  (0) 2023.05.20
c에서 rust 호출하기  (0) 2023.05.11
rust if문  (0) 2023.05.11
rust rustup doc  (0) 2023.05.10
rust cargo new를 통한 프로젝트 생성  (0) 2023.05.09
Posted by 구차니
Programming/rust2023. 5. 11. 10:07

golang과 비슷한데

그럼에도 불구하고 최소한 condition 부분의 괄호를 경로를 띄우지 error는 아니고

중괄호 위치는 마음대로 설정할 수 있다.

 

$ cat main.rs 
fn main() 
{
let number = 3;

if (number < 5)
{
println!("condition was true");
}
else
{
println!("condition was false");
}
}

 

$ rustc main.rs 
warning: unnecessary parentheses around `if` condition
 --> main.rs:5:6
  |
5 |         if (number < 5)
  |            ^          ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
5 -         if (number < 5)
5 +         if number < 5
  |

warning: 1 warning emitted

 

[링크 : https://doc.rust-lang.org/book/ch03-05-control-flow.html]

'Programming > rust' 카테고리의 다른 글

c에서 rust 호출하기  (0) 2023.05.11
rust 실행파일  (0) 2023.05.11
rust rustup doc  (0) 2023.05.10
rust cargo new를 통한 프로젝트 생성  (0) 2023.05.09
rust 와 main.rs  (0) 2023.05.09
Posted by 구차니
Programming/rust2023. 5. 10. 22:49

도움말 문서 보여줌

Welcome to an overview of the documentation provided by the Rust project. This page contains links to various helpful references, most of which are available offline (if opened with rustup doc).

 

'Programming > rust' 카테고리의 다른 글

rust 실행파일  (0) 2023.05.11
rust if문  (0) 2023.05.11
rust cargo new를 통한 프로젝트 생성  (0) 2023.05.09
rust 와 main.rs  (0) 2023.05.09
rust 문서 다운로드하기(cargo)  (0) 2023.05.09
Posted by 구차니
Programming/rust2023. 5. 9. 13:32

msvc 설치하고 하니 정상적으로 잘 빌드 된다.

C:\Users\user\Desktop\study\rust\hello_cargo>cargo build
   Compiling hello_cargo v0.1.0 (C:\Users\free\Desktop\study\rust\hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 1.04s

---

에라이.. 역시 윈도우는 취미/개발용이 아니라 엔터테인먼트 용이었나!? (뜬금없이 분노중)

C:\Users\user\Desktop\study\rust>cargo new hello_cargo
     Created binary (application) `hello_cargo` package

C:\Users\user\Desktop\study\rust\hello_cargo> cd hello_cargo

C:\Users\user\Desktop\study\rust\hello_cargo>cargo build
   Compiling hello_cargo v0.1.0 (C:\Users\free\Desktop\study\rust\hello_cargo)
error: linker `link.exe` not found
  |
  = note: program not found

note: the msvc targets depend on the msvc linker but `link.exe` was not found

note: please ensure that Visual Studio 2017 or later, or Build Tools for Visual Studio were installed with the Visual C++ option.

note: VS Code is a different product, and is not sufficient.

error: could not compile `hello_cargo` due to previous error

[링크 : https://doc.rust-lang.org/book/ch01-03-hello-cargo.html]

 

cargo.toml 내용은 평범한(?) ini 스타일이네

[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

 

.gitignore 파일은 /target 인걸 봐서는 /src 에는 소스 /target에는 바이너리가 생성되는 구조인듯

/target

'Programming > rust' 카테고리의 다른 글

rust if문  (0) 2023.05.11
rust rustup doc  (0) 2023.05.10
rust 와 main.rs  (0) 2023.05.09
rust 문서 다운로드하기(cargo)  (0) 2023.05.09
rust in windows  (0) 2023.05.09
Posted by 구차니
Programming/rust2023. 5. 9. 13:21

러스트니까 확장자가 rs인가..

함수는 fn 키워드로 구분하는 듯.

C:\Users\user\Desktop\study\rust>type main.rs
fn main() {
    println!("Hello, world!");
}

 

처음해보려는데 머가 잘 못 깔린걸까.. -_-

C:\Users\user\Desktop\study\rust>rustc main.rs
error: linker `link.exe` not found
  |
  = note: program not found

note: the msvc targets depend on the msvc linker but `link.exe` was not found

note: please ensure that Visual Studio 2017 or later, or Build Tools for Visual Studio were installed with the Visual C++ option.

note: VS Code is a different product, and is not sufficient.

error: aborting due to previous error

 

먼가 열심히 object 파일은 생성하는데 linker가 없어서 실행파일을 만드는건 실패한 듯.

C:\Users\user\Desktop\study\rust>dir
 C 드라이브의 볼륨에는 이름이 없습니다.
 볼륨 일련 번호: 38F7-6D98

 C:\Users\user\Desktop\study\rust 디렉터리

2023-05-09  오후 01:18    <DIR>          .
2023-05-09  오후 01:18    <DIR>          ..
2023-05-09  오후 01:18             1,224 main.398lq81sg45tyvtc.rcgu.o
2023-05-09  오후 01:18               508 main.main.b8591b61-cgu.0.rcgu.o
2023-05-09  오후 01:18             1,870 main.main.b8591b61-cgu.1.rcgu.o
2023-05-09  오후 01:18             1,651 main.main.b8591b61-cgu.2.rcgu.o
2023-05-09  오후 01:18               802 main.main.b8591b61-cgu.3.rcgu.o
2023-05-09  오후 01:18             2,396 main.main.b8591b61-cgu.4.rcgu.o
2023-05-09  오후 01:18             1,708 main.main.b8591b61-cgu.5.rcgu.o
2023-05-09  오후 01:18                46 main.rs
               8개 파일              10,205 바이트
               2개 디렉터리  127,890,599,936 바이트 남음

 

 

+

msvc 설치하고 나서 rustc 해서 빌드하니 아래와 같이 익숙한(!) pdb와 exe가 나온다.

C:\Users\user\Desktop\study\rust>rustc main.rs

C:\Users\user\Desktop\study\rust>dir
 C 드라이브의 볼륨에는 이름이 없습니다.
 볼륨 일련 번호: 38F7-6D98

 C:\Users\user\Desktop\study\rust 디렉터리

2023-05-09  오후 11:37    <DIR>          .
2023-05-09  오후 11:37    <DIR>          ..
2023-05-09  오후 01:29    <DIR>          hello_cargo
2023-05-09  오후 11:37           163,840 main.exe
2023-05-09  오후 11:37         1,355,776 main.pdb
2023-05-09  오후 01:18                46 main.rs
2023-05-09  오후 01:34        16,904,109 The Rust Programming Language.pdf
               4개 파일          18,423,771 바이트
               3개 디렉터리  121,282,232,320 바이트 남음

'Programming > rust' 카테고리의 다른 글

rust if문  (0) 2023.05.11
rust rustup doc  (0) 2023.05.10
rust cargo new를 통한 프로젝트 생성  (0) 2023.05.09
rust 문서 다운로드하기(cargo)  (0) 2023.05.09
rust in windows  (0) 2023.05.09
Posted by 구차니