AI來實現程式碼轉換!Python轉Java,Java轉Go不在話下?

程式猿DD發表於2023-03-12

今天看到個有趣的網站,給大家分享一下。

該網站的功能很神奇,可以實現程式語言的轉化。感覺在一些場景之下還是有點作用的,比如你原來跟我一樣是做Java的,因為工作需要突然轉Go。這個時候用你Java的經驗 + 這個工具,或許可以起到一定的幫助作用。

工具的使用也很簡單,只需要在左側黏貼你想轉換的原始程式碼,然後點選CONVERT CODE,右側輸入框就會轉換成目的碼:

像下面這樣更加複雜的程式碼轉換也是不在話下:

const fs = require('fs');
 const AWS = require('aws-sdk');
 
 const s3 = new AWS.S3({
     accessKeyId: process.env.AWS_ACCESS_KEY,
     secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
 });
 
 const fileName = 'contacts.csv';
 
 const uploadFile = () => {
   fs.readFile(fileName, (err, data) => {
       if (err) throw err;
       const params = {
           Bucket: 'testBucket', // pass your bucket name
           Key: 'contacts.csv', // file will be saved as testBucket/contacts.csv
           Body: JSON.stringify(data, null, 2)
       };
       s3.upload(params, function(s3Err, data) {
           if (s3Err) throw s3Err
       });
   });
 };
 
 uploadFile();

可以看到其中還包含了aws的內容,居然也能順利的轉成Java

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;

public class UploadObject {

    public static void main(String[] args) throws IOException {
        String clientRegion = "*** Client region ***";
        String bucketName = "*** Bucket name ***";
        String stringObjKeyName = "*** String object key name ***";
        String fileObjKeyName = "*** File object key name ***";
        String fileName = "*** Path to file to upload ***";

        try {
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(clientRegion)
                    .withCredentials(new ProfileCredentialsProvider())
                    .build();

            // Upload a text string as a new object.
            s3Client.putObject(bucketName, stringObjKeyName, "Uploaded String Object");

            // Upload a file as a new object with ContentType and title specified.
            PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType("plain/text");
            metadata.addUserMetadata("x-amz-meta-title", "someTitle");
            request.setMetadata(metadata);
            s3Client.putObject(request);
        }
        catch(AmazonServiceException e) {
            // The call was transmitted successfully, but Amazon S3 couldn't process 
            // it, so it returned an error response.
            e.printStackTrace();
        }
        catch(SdkClientException e) {
            // Amazon S3 couldn't be contacted for a response, or the client
            // couldn't parse the response from Amazon S3.
            e.printStackTrace();
        }
    }
}

根據官方說明,該工具也是透過AI實現的,是不是很神奇呢?這個到底是怎麼實現的呢?有了解的小夥伴留言區一起探討下吧!

歡迎關注我的公眾號:程式猿DD。第一時間瞭解前沿行業訊息、分享深度技術乾貨、獲取優質學習資源

相關文章